+ case ARGP_KEY_ARG:
+ /* File name or non-parsed option, because of ARGP_IN_ORDER */
+ name_add_name (arg, MAKE_INCL_OPTIONS (args));
+ args->input_files = true;
+ break;
+
+ case 'A':
+ set_subcommand_option (CAT_SUBCOMMAND);
+ break;
+
+ case 'b':
+ {
+ uintmax_t u;
+ if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
+ && u == (blocking_factor = u)
+ && 0 < blocking_factor
+ && u == (record_size = u * BLOCKSIZE) / BLOCKSIZE))
+ USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
+ _("Invalid blocking factor")));
+ }
+ break;
+
+ case 'B':
+ /* Try to reblock input records. For reading 4.2BSD pipes. */
+
+ /* It would surely make sense to exchange -B and -R, but it seems
+ that -B has been used for a long while in Sun tar and most
+ BSD-derived systems. This is a consequence of the block/record
+ terminology confusion. */
+
+ read_full_records_option = true;
+ break;
+
+ case 'c':
+ set_subcommand_option (CREATE_SUBCOMMAND);
+ break;
+
+ case 'C':
+ name_add_dir (arg);
+ break;
+
+ case 'd':
+ set_subcommand_option (DIFF_SUBCOMMAND);
+ break;
+
+ case 'f':
+ if (archive_names == allocated_archive_names)
+ archive_name_array = x2nrealloc (archive_name_array,
+ &allocated_archive_names,
+ sizeof (archive_name_array[0]));
+
+ archive_name_array[archive_names++] = arg;
+ break;
+
+ case 'F':
+ /* Since -F is only useful with -M, make it implied. Run this
+ script at the end of each tape. */
+
+ info_script_option = arg;
+ multi_volume_option = true;
+ break;
+
+ case 'g':
+ listed_incremental_option = arg;
+ after_date_option = true;
+ /* Fall through. */
+
+ case 'G':
+ /* We are making an incremental dump (FIXME: are we?); save
+ directories at the beginning of the archive, and include in each
+ directory its contents. */
+
+ incremental_option = true;
+ break;
+
+ case 'h':
+ /* Follow symbolic links. */
+ dereference_option = true;
+ break;
+
+ case 'i':
+ /* Ignore zero blocks (eofs). This can't be the default,
+ because Unix tar writes two blocks of zeros, then pads out
+ the record with garbage. */
+
+ ignore_zeros_option = true;
+ break;
+
+ case 'I':
+ USAGE_ERROR ((0, 0,
+ _("Warning: the -I option is not supported;"
+ " perhaps you meant -j or -T?")));
+ break;
+
+ case 'j':
+ set_use_compress_program_option ("bzip2");
+ break;
+
+ case 'k':
+ /* Don't replace existing files. */
+ old_files_option = KEEP_OLD_FILES;
+ break;
+
+ case 'K':
+ starting_file_option = true;
+ addname (arg, 0);
+ break;
+
+ case ONE_FILE_SYSTEM_OPTION:
+ /* When dumping directories, don't dump files/subdirectories
+ that are on other filesystems. */
+ one_file_system_option = true;
+ break;
+
+ case 'l':
+ check_links_option = 1;
+ break;
+
+ case 'L':
+ {
+ uintmax_t u;
+ if (xstrtoumax (arg, 0, 10, &u, "") != LONGINT_OK)
+ USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
+ _("Invalid tape length")));
+ tape_length_option = 1024 * (tarlong) u;
+ multi_volume_option = true;
+ }
+ break;
+
+ case 'm':
+ touch_option = true;
+ break;
+
+ case 'M':
+ /* Make multivolume archive: when we can't write any more into
+ the archive, re-open it, and continue writing. */
+
+ multi_volume_option = true;
+ break;
+
+ case 'n':
+ seekable_archive = true;
+ break;
+
+ case 'N':
+ after_date_option = true;
+ /* Fall through. */
+
+ case NEWER_MTIME_OPTION:
+ if (NEWER_OPTION_INITIALIZED (newer_mtime_option))
+ USAGE_ERROR ((0, 0, _("More than one threshold date")));
+
+ if (FILE_SYSTEM_PREFIX_LEN (arg) != 0
+ || ISSLASH (*arg)
+ || *arg == '.')
+ {
+ struct stat st;
+ if (deref_stat (dereference_option, arg, &st) != 0)
+ {
+ stat_error (arg);
+ USAGE_ERROR ((0, 0, _("Date sample file not found")));
+ }
+ newer_mtime_option = get_stat_mtime (&st);
+ }
+ else
+ {
+ if (! get_date (&newer_mtime_option, arg, NULL))
+ {
+ WARN ((0, 0, _("Substituting %s for unknown date format %s"),
+ tartime (newer_mtime_option, false), quote (arg)));
+ newer_mtime_option.tv_nsec = 0;
+ }
+ else
+ args->textual_date_option = arg;
+ }
+
+ break;
+
+ case 'o':
+ args->o_option = true;
+ break;
+
+ case 'O':
+ to_stdout_option = true;
+ break;
+
+ case 'p':
+ same_permissions_option = true;
+ break;
+
+ case 'P':
+ absolute_names_option = true;
+ break;
+
+ case 'r':
+ set_subcommand_option (APPEND_SUBCOMMAND);
+ break;
+
+ case 'R':
+ /* Print block numbers for debugging bad tar archives. */
+
+ /* It would surely make sense to exchange -B and -R, but it seems
+ that -B has been used for a long while in Sun tar ans most
+ BSD-derived systems. This is a consequence of the block/record
+ terminology confusion. */
+
+ block_number_option = true;
+ break;
+
+ case 's':
+ /* Names to extr are sorted. */
+
+ same_order_option = true;
+ break;
+
+ case 'S':
+ sparse_option = true;
+ break;
+
+ case 't':
+ set_subcommand_option (LIST_SUBCOMMAND);
+ verbose_option++;
+ break;
+
+ case TEST_LABEL_OPTION:
+ set_subcommand_option (LIST_SUBCOMMAND);
+ test_label_option = true;
+ break;
+
+ case 'T':
+ update_argv (arg, state);
+ /* Indicate we've been given -T option. This is for backward
+ compatibility only, so that `tar cfT archive /dev/null will
+ succeed */
+ files_from_option = true;
+ break;
+
+ case 'u':
+ set_subcommand_option (UPDATE_SUBCOMMAND);
+ break;
+
+ case 'U':
+ old_files_option = UNLINK_FIRST_OLD_FILES;
+ break;
+
+ case UTC_OPTION:
+ utc_option = true;
+ break;
+
+ case 'v':
+ verbose_option++;
+ break;
+
+ case 'V':
+ volume_label_option = arg;
+ break;
+
+ case 'w':
+ interactive_option = true;
+ break;
+
+ case 'W':
+ verify_option = true;
+ break;
+
+ case 'x':
+ set_subcommand_option (EXTRACT_SUBCOMMAND);
+ break;
+
+ case 'X':
+ if (add_exclude_file (add_exclude, excluded, arg,
+ MAKE_EXCL_OPTIONS (args), '\n')
+ != 0)
+ {
+ int e = errno;
+ FATAL_ERROR ((0, e, "%s", quotearg_colon (arg)));
+ }
+ break;
+
+ case 'z':
+ set_use_compress_program_option ("gzip");
+ break;
+
+ case 'Z':
+ set_use_compress_program_option ("compress");
+ break;
+
+ case ANCHORED_OPTION:
+ args->matching_flags |= EXCLUDE_ANCHORED;
+ break;
+
+ case ATIME_PRESERVE_OPTION:
+ atime_preserve_option =
+ (arg
+ ? XARGMATCH ("--atime-preserve", arg,
+ atime_preserve_args, atime_preserve_types)
+ : replace_atime_preserve);
+ if (! O_NOATIME && atime_preserve_option == system_atime_preserve)
+ FATAL_ERROR ((0, 0,
+ _("--atime-preserve='system' is not supported"
+ " on this platform")));
+ break;
+
+ case CHECKPOINT_OPTION:
+ checkpoint_option = true;
+ break;
+
+ case BACKUP_OPTION:
+ backup_option = true;
+ if (arg)
+ args->version_control_string = arg;
+ break;
+
+ case DELAY_DIRECTORY_RESTORE_OPTION:
+ delay_directory_restore_option = true;
+ break;
+
+ case NO_DELAY_DIRECTORY_RESTORE_OPTION:
+ delay_directory_restore_option = false;
+ break;
+
+ case DELETE_OPTION:
+ set_subcommand_option (DELETE_SUBCOMMAND);
+ break;
+
+ case EXCLUDE_OPTION:
+ add_exclude (excluded, arg, MAKE_EXCL_OPTIONS (args));
+ break;
+
+ case EXCLUDE_CACHES_OPTION:
+ exclude_caches_option = true;
+ break;
+
+ case FORCE_LOCAL_OPTION:
+ force_local_option = true;
+ break;
+
+ case 'H':
+ set_archive_format (arg);
+ break;
+
+ case INDEX_FILE_OPTION:
+ index_file_name = arg;
+ break;
+
+ case IGNORE_CASE_OPTION:
+ args->matching_flags |= FNM_CASEFOLD;
+ break;
+
+ case IGNORE_COMMAND_ERROR_OPTION:
+ ignore_command_error_option = true;
+ break;
+
+ case IGNORE_FAILED_READ_OPTION:
+ ignore_failed_read_option = true;
+ break;
+
+ case KEEP_NEWER_FILES_OPTION:
+ old_files_option = KEEP_NEWER_FILES;
+ break;
+
+ case GROUP_OPTION:
+ if (! (strlen (arg) < GNAME_FIELD_SIZE
+ && gname_to_gid (arg, &group_option)))
+ {
+ uintmax_t g;
+ if (xstrtoumax (arg, 0, 10, &g, "") == LONGINT_OK
+ && g == (gid_t) g)
+ group_option = g;
+ else
+ FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
+ _("%s: Invalid group")));
+ }
+ break;
+
+ case MODE_OPTION:
+ mode_option = mode_compile (arg);
+ if (!mode_option)
+ FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
+ initial_umask = umask (0);
+ umask (initial_umask);
+ break;
+
+ case NO_ANCHORED_OPTION:
+ args->include_anchored = 0; /* Clear the default for comman line args */
+ args->matching_flags &= ~ EXCLUDE_ANCHORED;
+ break;
+
+ case NO_IGNORE_CASE_OPTION:
+ args->matching_flags &= ~ FNM_CASEFOLD;
+ break;
+
+ case NO_IGNORE_COMMAND_ERROR_OPTION:
+ ignore_command_error_option = false;
+ break;
+
+ case NO_OVERWRITE_DIR_OPTION:
+ old_files_option = NO_OVERWRITE_DIR_OLD_FILES;
+ break;
+
+ case NO_QUOTE_CHARS_OPTION:
+ for (;*arg; arg++)
+ set_char_quoting (NULL, *arg, 0);
+ break;
+
+ case NO_WILDCARDS_OPTION:
+ args->wildcards = disable_wildcards;
+ break;
+
+ case NO_WILDCARDS_MATCH_SLASH_OPTION:
+ args->matching_flags |= FNM_FILE_NAME;
+ break;
+
+ case NULL_OPTION:
+ filename_terminator = '\0';
+ break;
+
+ case NUMERIC_OWNER_OPTION:
+ numeric_owner_option = true;
+ break;
+
+ case OCCURRENCE_OPTION:
+ if (!arg)
+ occurrence_option = 1;
+ else
+ {
+ uintmax_t u;
+ if (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK)
+ occurrence_option = u;
+ else
+ FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
+ _("Invalid number")));
+ }
+ break;
+
+ case OVERWRITE_OPTION:
+ old_files_option = OVERWRITE_OLD_FILES;
+ break;
+
+ case OWNER_OPTION:
+ if (! (strlen (arg) < UNAME_FIELD_SIZE
+ && uname_to_uid (arg, &owner_option)))
+ {
+ uintmax_t u;
+ if (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
+ && u == (uid_t) u)
+ owner_option = u;
+ else
+ FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
+ _("Invalid owner")));
+ }
+ break;
+
+ case QUOTE_CHARS_OPTION:
+ for (;*arg; arg++)
+ set_char_quoting (NULL, *arg, 1);
+ break;
+
+ case QUOTING_STYLE_OPTION:
+ tar_set_quoting_style (arg);
+ break;
+
+ case PAX_OPTION:
+ args->pax_option = true;
+ xheader_set_option (arg);
+ break;
+
+ case POSIX_OPTION:
+ set_archive_format ("posix");
+ break;
+
+ case PRESERVE_OPTION:
+ /* FIXME: What it is good for? */
+ same_permissions_option = true;
+ same_order_option = true;
+ break;
+
+ case RECORD_SIZE_OPTION:
+ {
+ uintmax_t u;
+ if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
+ && u == (size_t) u))
+ USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
+ _("Invalid record size")));
+ record_size = u;
+ if (record_size % BLOCKSIZE != 0)
+ USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
+ BLOCKSIZE));
+ blocking_factor = record_size / BLOCKSIZE;
+ }
+ break;
+
+ case RECURSIVE_UNLINK_OPTION:
+ recursive_unlink_option = true;
+ break;
+
+ case REMOVE_FILES_OPTION:
+ remove_files_option = true;
+ break;
+
+ case RESTRICT_OPTION:
+ restrict_option = true;
+ break;
+
+ case RMT_COMMAND_OPTION:
+ rmt_command = arg;
+ break;
+
+ case RSH_COMMAND_OPTION:
+ rsh_command_option = arg;
+ break;
+
+ case SHOW_DEFAULTS_OPTION:
+ show_default_settings (stdout);
+ close_stdout ();
+ exit (0);
+
+ case STRIP_COMPONENTS_OPTION:
+ {
+ uintmax_t u;
+ if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
+ && u == (size_t) u))
+ USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
+ _("Invalid number of elements")));
+ strip_name_components = u;
+ }
+ break;
+
+ case SHOW_OMITTED_DIRS_OPTION:
+ show_omitted_dirs_option = true;
+ break;
+
+ case SHOW_TRANSFORMED_NAMES_OPTION:
+ show_transformed_names_option = true;
+ break;
+
+ case SUFFIX_OPTION:
+ backup_option = true;
+ args->backup_suffix_string = arg;
+ break;
+
+ case TO_COMMAND_OPTION:
+ if (to_command_option)
+ USAGE_ERROR ((0, 0, _("Only one --to-command option allowed")));
+ to_command_option = arg;
+ break;
+
+ case TOTALS_OPTION:
+ totals_option = true;
+ break;
+
+ case TRANSFORM_OPTION:
+ set_transform_expr (arg);
+ break;
+
+ case USE_COMPRESS_PROGRAM_OPTION:
+ set_use_compress_program_option (arg);
+ break;
+
+ case VOLNO_FILE_OPTION:
+ volno_file_option = arg;
+ break;
+
+ case WILDCARDS_OPTION:
+ args->wildcards = enable_wildcards;
+ break;
+
+ case WILDCARDS_MATCH_SLASH_OPTION:
+ args->matching_flags &= ~ FNM_FILE_NAME;
+ break;
+
+ case NO_RECURSION_OPTION:
+ recursion_option = 0;
+ break;
+
+ case NO_SAME_OWNER_OPTION:
+ same_owner_option = -1;
+ break;
+
+ case NO_SAME_PERMISSIONS_OPTION:
+ same_permissions_option = -1;
+ break;
+
+ case RECURSION_OPTION:
+ recursion_option = FNM_LEADING_DIR;
+ break;
+
+ case SAME_OWNER_OPTION:
+ same_owner_option = 1;
+ break;
+
+ case UNQUOTE_OPTION:
+ unquote_option = true;
+ break;
+
+ case NO_UNQUOTE_OPTION:
+ unquote_option = false;
+ break;
+
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+
+#ifdef DEVICE_PREFIX
+ {
+ int device = key - '0';
+ int density;
+ static char buf[sizeof DEVICE_PREFIX + 10];
+ char *cursor;
+
+ if (arg[1])
+ argp_error (state, _("Malformed density argument: %s"), quote (arg));
+
+ strcpy (buf, DEVICE_PREFIX);
+ cursor = buf + strlen (buf);
+
+#ifdef DENSITY_LETTER
+
+ sprintf (cursor, "%d%c", device, arg[0]);
+
+#else /* not DENSITY_LETTER */
+
+ switch (arg[0])
+ {
+ case 'l':
+ device += LOW_DENSITY_NUM;
+ break;
+
+ case 'm':
+ device += MID_DENSITY_NUM;
+ break;
+
+ case 'h':
+ device += HIGH_DENSITY_NUM;
+ break;
+
+ default:
+ argp_error (state, _("Unknown density: `%c'"), arg[0]);
+ }
+ sprintf (cursor, "%d", device);
+
+#endif /* not DENSITY_LETTER */
+
+ if (archive_names == allocated_archive_names)
+ archive_name_array = x2nrealloc (archive_name_array,
+ &allocated_archive_names,
+ sizeof (archive_name_array[0]));
+ archive_name_array[archive_names++] = xstrdup (buf);
+ }
+ break;
+
+#else /* not DEVICE_PREFIX */
+
+ argp_error (state,
+ _("Options `-[0-7][lmh]' not supported by *this* tar"));
+
+#endif /* not DEVICE_PREFIX */
+
+ case '?':
+ state->flags |= ARGP_NO_EXIT;
+ argp_state_help (state, state->out_stream,
+ ARGP_HELP_STD_HELP & ~ARGP_HELP_BUG_ADDR);
+ fprintf (state->out_stream, "\n%s\n\n",
+ _("Valid arguments for --quoting-style options are:"));
+ tar_list_quoting_styles (state->out_stream, " ");
+
+ fprintf (state->out_stream, _("\n*This* tar defaults to:\n"));
+ show_default_settings (state->out_stream);
+ fprintf (state->out_stream, "\n");
+ fprintf (state->out_stream, _("Report bugs to %s.\n"),
+ argp_program_bug_address);
+ close_stdout ();
+ exit (0);
+
+ case USAGE_OPTION:
+ argp_state_help (state, state->out_stream, ARGP_HELP_USAGE);
+ close_stdout ();
+ exit (0);
+
+ case VERSION_OPTION:
+ version_etc (state->out_stream, "tar", PACKAGE_NAME, VERSION,
+ "John Gilmore", "Jay Fenlason", (char *) NULL);
+ close_stdout ();
+ exit (0);
+
+ case HANG_OPTION:
+ _argp_hang = atoi (arg ? arg : "3600");
+ while (_argp_hang-- > 0)
+ sleep (1);
+ break;
+
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ return 0;
+}
+
+static struct argp argp = {
+ options,
+ parse_opt,
+ N_("[FILE]..."),
+ doc,
+ NULL,
+ NULL,
+ NULL
+};
+
+void
+usage (int status)
+{
+ argp_help (&argp, stderr, ARGP_HELP_SEE, (char*) program_name);
+ close_stdout ();
+ exit (status);
+}
+
+/* Parse the options for tar. */
+
+static struct argp_option *
+find_argp_option (struct argp_option *options, int letter)
+{
+ for (;
+ !(options->name == NULL
+ && options->key == 0
+ && options->arg == 0
+ && options->flags == 0
+ && options->doc == NULL); options++)
+ if (options->key == letter)
+ return options;
+ return NULL;
+}
+
+static void
+decode_options (int argc, char **argv)
+{
+ int index;
+ struct tar_args args;
+
+ /* Set some default option values. */
+ args.textual_date_option = NULL;
+ args.wildcards = default_wildcards;
+ args.matching_flags = 0;
+ args.include_anchored = EXCLUDE_ANCHORED;
+ args.o_option = false;
+ args.pax_option = false;
+ args.backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
+ args.version_control_string = 0;
+ args.input_files = false;
+
+ subcommand_option = UNKNOWN_SUBCOMMAND;
+ archive_format = DEFAULT_FORMAT;
+ blocking_factor = DEFAULT_BLOCKING;
+ record_size = DEFAULT_BLOCKING * BLOCKSIZE;
+ excluded = new_exclude ();
+ newer_mtime_option.tv_sec = TYPE_MINIMUM (time_t);
+ newer_mtime_option.tv_nsec = -1;
+ recursion_option = FNM_LEADING_DIR;
+ unquote_option = true;
+
+ owner_option = -1;
+ group_option = -1;
+
+ /* Convert old-style tar call by exploding option element and rearranging
+ options accordingly. */
+
+ if (argc > 1 && argv[1][0] != '-')
+ {
+ int new_argc; /* argc value for rearranged arguments */
+ char **new_argv; /* argv value for rearranged arguments */
+ char *const *in; /* cursor into original argv */
+ char **out; /* cursor into rearranged argv */
+ const char *letter; /* cursor into old option letters */
+ char buffer[3]; /* constructed option buffer */
+
+ /* Initialize a constructed option. */
+
+ buffer[0] = '-';
+ buffer[2] = '\0';
+
+ /* Allocate a new argument array, and copy program name in it. */
+
+ new_argc = argc - 1 + strlen (argv[1]);
+ new_argv = xmalloc ((new_argc + 1) * sizeof (char *));
+ in = argv;
+ out = new_argv;
+ *out++ = *in++;
+
+ /* Copy each old letter option as a separate option, and have the
+ corresponding argument moved next to it. */
+
+ for (letter = *in++; *letter; letter++)
+ {
+ struct argp_option *opt;
+
+ buffer[1] = *letter;
+ *out++ = xstrdup (buffer);
+ opt = find_argp_option (options, *letter);
+ if (opt && opt->arg)
+ {
+ if (in < argv + argc)
+ *out++ = *in++;
+ else
+ USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
+ *letter));
+ }
+ }
+
+ /* Copy all remaining options. */
+
+ while (in < argv + argc)
+ *out++ = *in++;
+ *out = 0;
+
+ /* Replace the old option list by the new one. */
+
+ argc = new_argc;
+ argv = new_argv;
+ }
+
+ /* Parse all options and non-options as they appear. */
+
+ prepend_default_options (getenv ("TAR_OPTIONS"), &argc, &argv);
+
+ if (argp_parse (&argp, argc, argv, ARGP_IN_ORDER|ARGP_NO_HELP,
+ &index, &args))
+ exit (TAREXIT_FAILURE);
+
+
+ /* Special handling for 'o' option:
+
+ GNU tar used to say "output old format".
+ UNIX98 tar says don't chown files after extracting (we use
+ "--no-same-owner" for this).
+
+ The old GNU tar semantics is retained when used with --create
+ option, otherwise UNIX98 semantics is assumed */
+
+ if (args.o_option)
+ {
+ if (subcommand_option == CREATE_SUBCOMMAND)
+ {
+ /* GNU Tar <= 1.13 compatibility */
+ set_archive_format ("v7");
+ }
+ else
+ {
+ /* UNIX98 compatibility */
+ same_owner_option = -1;
+ }
+ }
+
+ /* Handle operands after any "--" argument. */
+ for (; index < argc; index++)
+ {
+ name_add_name (argv[index], MAKE_INCL_OPTIONS (&args));
+ args.input_files = true;
+ }
+
+ /* Warn about implicit use of the wildcards in command line arguments.
+ See TODO */
+ warn_regex_usage = args.wildcards == default_wildcards;
+
+ /* Derive option values and check option consistency. */
+
+ if (archive_format == DEFAULT_FORMAT)
+ {
+ if (args.pax_option)
+ archive_format = POSIX_FORMAT;
+ else
+ archive_format = DEFAULT_ARCHIVE_FORMAT;
+ }
+
+ if ((volume_label_option && subcommand_option == CREATE_SUBCOMMAND)
+ || incremental_option
+ || multi_volume_option
+ || sparse_option)
+ assert_format (FORMAT_MASK (OLDGNU_FORMAT)
+ | FORMAT_MASK (GNU_FORMAT)
+ | FORMAT_MASK (POSIX_FORMAT));
+
+ if (multi_volume_option
+ && archive_format == POSIX_FORMAT
+ && subcommand_option == CREATE_SUBCOMMAND
+ && !tape_length_option)
+ USAGE_ERROR ((0, 0,
+ _("creating multi-volume archives in posix format requires using --tape-length (-L) option")));
+
+ if (occurrence_option)
+ {
+ if (!args.input_files)
+ USAGE_ERROR ((0, 0,
+ _("--occurrence is meaningless without a file list")));
+ if (subcommand_option != DELETE_SUBCOMMAND
+ && subcommand_option != DIFF_SUBCOMMAND
+ && subcommand_option != EXTRACT_SUBCOMMAND
+ && subcommand_option != LIST_SUBCOMMAND)
+ USAGE_ERROR ((0, 0,
+ _("--occurrence cannot be used in the requested operation mode")));
+ }
+
+ if (seekable_archive && subcommand_option == DELETE_SUBCOMMAND)
+ {
+ /* The current code in delete.c is based on the assumption that
+ skip_member() reads all data from the archive. So, we should
+ make sure it won't use seeks. On the other hand, the same code
+ depends on the ability to backspace a record in the archive,
+ so setting seekable_archive to false is technically incorrect.
+ However, it is tested only in skip_member(), so it's not a
+ problem. */
+ seekable_archive = false;
+ }
+
+ if (archive_names == 0)
+ {
+ /* If no archive file name given, try TAPE from the environment, or
+ else, DEFAULT_ARCHIVE from the configuration process. */
+
+ archive_names = 1;
+ archive_name_array[0] = getenv ("TAPE");
+ if (! archive_name_array[0])
+ archive_name_array[0] = DEFAULT_ARCHIVE;
+ }
+
+ /* Allow multiple archives only with `-M'. */
+
+ if (archive_names > 1 && !multi_volume_option)
+ USAGE_ERROR ((0, 0,
+ _("Multiple archive files require `-M' option")));
+
+ if (listed_incremental_option
+ && NEWER_OPTION_INITIALIZED (newer_mtime_option))
+ USAGE_ERROR ((0, 0,
+ _("Cannot combine --listed-incremental with --newer")));
+
+ if (volume_label_option)
+ {
+ if (archive_format == GNU_FORMAT || archive_format == OLDGNU_FORMAT)
+ {
+ size_t volume_label_max_len =
+ (sizeof current_header->header.name
+ - 1 /* for trailing '\0' */
+ - (multi_volume_option
+ ? (sizeof " Volume "
+ - 1 /* for null at end of " Volume " */
+ + INT_STRLEN_BOUND (int) /* for volume number */
+ - 1 /* for sign, as 0 <= volno */)
+ : 0));
+ if (volume_label_max_len < strlen (volume_label_option))
+ USAGE_ERROR ((0, 0,
+ ngettext ("%s: Volume label is too long (limit is %lu byte)",
+ "%s: Volume label is too long (limit is %lu bytes)",
+ volume_label_max_len),
+ quotearg_colon (volume_label_option),
+ (unsigned long) volume_label_max_len));
+ }
+ /* else FIXME
+ Label length in PAX format is limited by the volume size. */
+ }
+
+ if (verify_option)
+ {
+ if (multi_volume_option)
+ USAGE_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
+ if (use_compress_program_option)
+ USAGE_ERROR ((0, 0, _("Cannot verify compressed archives")));
+ }
+
+ if (use_compress_program_option)
+ {
+ if (multi_volume_option)
+ USAGE_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
+ if (subcommand_option == UPDATE_SUBCOMMAND
+ || subcommand_option == APPEND_SUBCOMMAND
+ || subcommand_option == DELETE_SUBCOMMAND)
+ USAGE_ERROR ((0, 0, _("Cannot update compressed archives")));
+ if (subcommand_option == CAT_SUBCOMMAND)
+ USAGE_ERROR ((0, 0, _("Cannot concatenate compressed archives")));
+ }
+
+ /* It is no harm to use --pax-option on non-pax archives in archive
+ reading mode. It may even be useful, since it allows to override
+ file attributes from tar headers. Therefore I allow such usage.
+ --gray */
+ if (args.pax_option
+ && archive_format != POSIX_FORMAT
+ && (subcommand_option != EXTRACT_SUBCOMMAND
+ || subcommand_option != DIFF_SUBCOMMAND
+ || subcommand_option != LIST_SUBCOMMAND))
+ USAGE_ERROR ((0, 0, _("--pax-option can be used only on POSIX archives")));
+
+ /* If ready to unlink hierarchies, so we are for simpler files. */
+ if (recursive_unlink_option)
+ old_files_option = UNLINK_FIRST_OLD_FILES;
+
+
+ if (test_label_option)
+ {
+ /* --test-label is silent if the user has specified the label name to
+ compare against. */
+ if (!args.input_files)
+ verbose_option++;
+ }
+ else if (utc_option)
+ verbose_option = 2;
+
+ /* Forbid using -c with no input files whatsoever. Check that `-f -',
+ explicit or implied, is used correctly. */
+
+ switch (subcommand_option)
+ {
+ case CREATE_SUBCOMMAND:
+ if (!args.input_files && !files_from_option)
+ USAGE_ERROR ((0, 0,
+ _("Cowardly refusing to create an empty archive")));
+ break;
+
+ case EXTRACT_SUBCOMMAND:
+ case LIST_SUBCOMMAND:
+ case DIFF_SUBCOMMAND:
+ for (archive_name_cursor = archive_name_array;
+ archive_name_cursor < archive_name_array + archive_names;
+ archive_name_cursor++)
+ if (!strcmp (*archive_name_cursor, "-"))
+ request_stdin ("-f");
+ break;
+
+ case CAT_SUBCOMMAND:
+ case UPDATE_SUBCOMMAND:
+ case APPEND_SUBCOMMAND:
+ for (archive_name_cursor = archive_name_array;
+ archive_name_cursor < archive_name_array + archive_names;
+ archive_name_cursor++)
+ if (!strcmp (*archive_name_cursor, "-"))
+ USAGE_ERROR ((0, 0,
+ _("Options `-Aru' are incompatible with `-f -'")));
+
+ default:
+ break;
+ }
+
+ archive_name_cursor = archive_name_array;
+
+ /* Prepare for generating backup names. */
+
+ if (args.backup_suffix_string)
+ simple_backup_suffix = xstrdup (args.backup_suffix_string);
+
+ if (backup_option)
+ {
+ backup_type = xget_version ("--backup", args.version_control_string);
+ /* No backup is needed either if explicitely disabled or if
+ the extracted files are not being written to disk. */
+ if (backup_type == no_backups || EXTRACT_OVER_PIPE)
+ backup_option = false;
+ }
+
+ if (verbose_option && args.textual_date_option)
+ {
+ char const *treated_as = tartime (newer_mtime_option, true);
+ if (strcmp (args.textual_date_option, treated_as) != 0)
+ WARN ((0, 0, _("Treating date `%s' as %s"),
+ args.textual_date_option, treated_as));
+ }
+}
+
+\f
+/* Tar proper. */
+
+/* Main routine for tar. */
+int
+main (int argc, char **argv)
+{
+ set_start_time ();
+ program_name = argv[0];
+
+ setlocale (LC_ALL, "");
+ bindtextdomain (PACKAGE, LOCALEDIR);
+ textdomain (PACKAGE);
+
+ exit_failure = TAREXIT_FAILURE;
+ exit_status = TAREXIT_SUCCESS;
+ filename_terminator = '\n';
+ set_quoting_style (0, DEFAULT_QUOTING_STYLE);
+
+ /* Make sure we have first three descriptors available */
+ stdopen ();
+
+ /* Pre-allocate a few structures. */
+
+ allocated_archive_names = 10;
+ archive_name_array =
+ xmalloc (sizeof (const char *) * allocated_archive_names);
+ archive_names = 0;
+
+ obstack_init (&argv_stk);
+
+#ifdef SIGCHLD
+ /* System V fork+wait does not work if SIGCHLD is ignored. */
+ signal (SIGCHLD, SIG_DFL);
+#endif
+
+ /* Decode options. */
+
+ decode_options (argc, argv);
+ name_init ();
+
+ /* Main command execution. */
+
+ if (volno_file_option)
+ init_volume_number ();
+
+ switch (subcommand_option)
+ {
+ case UNKNOWN_SUBCOMMAND:
+ USAGE_ERROR ((0, 0,
+ _("You must specify one of the `-Acdtrux' options")));
+
+ case CAT_SUBCOMMAND:
+ case UPDATE_SUBCOMMAND:
+ case APPEND_SUBCOMMAND:
+ update_archive ();
+ break;
+
+ case DELETE_SUBCOMMAND:
+ delete_archive_members ();
+ break;
+
+ case CREATE_SUBCOMMAND:
+ create_archive ();
+ if (totals_option)
+ print_total_written ();
+ break;
+
+ case EXTRACT_SUBCOMMAND:
+ extr_init ();
+ read_and (extract_archive);
+
+ /* FIXME: should extract_finish () even if an ordinary signal is
+ received. */
+ extract_finish ();
+
+ break;
+
+ case LIST_SUBCOMMAND:
+ read_and (list_archive);
+ break;
+
+ case DIFF_SUBCOMMAND:
+ diff_init ();
+ read_and (diff_archive);
+ break;
+ }
+
+ if (check_links_option)
+ check_links ();
+
+ if (volno_file_option)
+ closeout_volume_number ();
+
+ /* Dispose of allocated memory, and return. */
+
+ free (archive_name_array);
+ name_term ();
+
+ if (stdlis == stdout)
+ close_stdout ();
+
+ if (exit_status == TAREXIT_FAILURE)
+ error (0, 0, _("Error exit delayed from previous errors"));
+ if (ferror (stderr) || fclose (stderr) != 0)
+ exit_status = TAREXIT_FAILURE;
+ return exit_status;
+}
+
+void
+tar_stat_init (struct tar_stat_info *st)
+{
+ memset (st, 0, sizeof (*st));
+}
+
+void
+tar_stat_destroy (struct tar_stat_info *st)
+{
+ free (st->orig_file_name);
+ free (st->file_name);
+ free (st->link_name);
+ free (st->uname);
+ free (st->gname);
+ free (st->sparse_map);
+ free (st->dumpdir);
+ memset (st, 0, sizeof (*st));
+}
+
+/* Format mask for all available formats that support nanosecond
+ timestamp resolution. */
+#define NS_PRECISION_FORMAT_MASK FORMAT_MASK (POSIX_FORMAT)
+
+/* Same as timespec_cmp, but ignore nanoseconds if current archive
+ format does not provide sufficient resolution. */
+int
+tar_timespec_cmp (struct timespec a, struct timespec b)
+{
+ if (!(FORMAT_MASK (current_format) & NS_PRECISION_FORMAT_MASK))
+ a.tv_nsec = b.tv_nsec = 0;
+ return timespec_cmp (a, b);