1 /* A tar (tape archiver) program.
2 Copyright 1988,92,93,94,95,96,97,99,2000,2001 Free Software Foundation, Inc.
3 Written by John Gilmore, starting 1985-08-25.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #if ! defined SIGCHLD && defined SIGCLD
26 # define SIGCHLD SIGCLD
29 /* The following causes "common.h" to produce definitions of all the global
30 variables, rather than just "extern" declarations of them. GNU tar does
31 depend on the system loader to preset all GLOBAL variables to neutral (or
32 zero) values; explicit initialization is usually not done. */
36 #include <localedir.h>
43 /* Local declarations. */
45 #ifndef DEFAULT_ARCHIVE
46 # define DEFAULT_ARCHIVE "tar.out"
49 #ifndef DEFAULT_BLOCKING
50 # define DEFAULT_BLOCKING 20
53 static void usage PARAMS ((int)) __attribute__ ((noreturn));
57 /* Name of option using stdin. */
58 static const char *stdin_used_by;
60 /* Doesn't return if stdin already requested. */
62 request_stdin (const char *option)
65 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
66 stdin_used_by, option));
68 stdin_used_by = option;
71 /* Returns true if and only if the user typed 'y' or 'Y'. */
73 confirm (const char *message_action, const char *message_name)
75 static FILE *confirm_file;
76 static int confirm_file_EOF;
80 if (archive == 0 || stdin_used_by)
82 confirm_file = fopen (TTY_NAME, "r");
84 open_fatal (TTY_NAME);
93 fprintf (stdlis, "%s %s?", message_action, quote (message_name));
97 int reply = confirm_file_EOF ? EOF : getc (confirm_file);
100 for (character = reply;
102 character = getc (confirm_file))
103 if (character == EOF)
105 confirm_file_EOF = 1;
106 fputc ('\n', stdlis);
110 return reply == 'y' || reply == 'Y';
116 /* For long options that unconditionally set a single flag, we have getopt
117 do it. For the others, we share the code for the equivalent short
118 named option, the name of which is stored in the otherwise-unused `val'
119 field of the `struct option'; for long options that have no equivalent
120 short option, we use non-characters as pseudo short options,
121 starting at CHAR_MAX + 1 and going upwards. */
125 BACKUP_OPTION = CHAR_MAX + 1,
139 USE_COMPRESS_PROGRAM_OPTION,
142 /* Some cleanup is being made in GNU tar long options. Using old names is
143 allowed for a while, but will also send a warning to stderr. Take old
144 names out in 1.14, or in summer 1997, whichever happens last. */
146 OBSOLETE_ABSOLUTE_NAMES,
147 OBSOLETE_BLOCK_COMPRESS,
148 OBSOLETE_BLOCKING_FACTOR,
149 OBSOLETE_BLOCK_NUMBER,
150 OBSOLETE_READ_FULL_RECORDS,
152 OBSOLETE_VERSION_CONTROL
155 /* If nonzero, display usage information and exit. */
156 static int show_help;
158 /* If nonzero, print the version on standard output and exit. */
159 static int show_version;
161 static struct option long_options[] =
163 {"absolute-names", no_argument, 0, 'P'},
164 {"absolute-paths", no_argument, 0, OBSOLETE_ABSOLUTE_NAMES},
165 {"after-date", required_argument, 0, 'N'},
166 {"append", no_argument, 0, 'r'},
167 {"atime-preserve", no_argument, &atime_preserve_option, 1},
168 {"backup", optional_argument, 0, BACKUP_OPTION},
169 {"block-compress", no_argument, 0, OBSOLETE_BLOCK_COMPRESS},
170 {"block-number", no_argument, 0, 'R'},
171 {"block-size", required_argument, 0, OBSOLETE_BLOCKING_FACTOR},
172 {"blocking-factor", required_argument, 0, 'b'},
173 {"bzip2", no_argument, 0, 'j'},
174 {"catenate", no_argument, 0, 'A'},
175 {"checkpoint", no_argument, &checkpoint_option, 1},
176 {"compare", no_argument, 0, 'd'},
177 {"compress", no_argument, 0, 'Z'},
178 {"concatenate", no_argument, 0, 'A'},
179 {"confirmation", no_argument, 0, 'w'},
180 /* FIXME: --selective as a synonym for --confirmation? */
181 {"create", no_argument, 0, 'c'},
182 {"delete", no_argument, 0, DELETE_OPTION},
183 {"dereference", no_argument, 0, 'h'},
184 {"diff", no_argument, 0, 'd'},
185 {"directory", required_argument, 0, 'C'},
186 {"exclude", required_argument, 0, EXCLUDE_OPTION},
187 {"exclude-from", required_argument, 0, 'X'},
188 {"extract", no_argument, 0, 'x'},
189 {"file", required_argument, 0, 'f'},
190 {"files-from", required_argument, 0, 'T'},
191 {"force-local", no_argument, &force_local_option, 1},
192 {"get", no_argument, 0, 'x'},
193 {"group", required_argument, 0, GROUP_OPTION},
194 {"gunzip", no_argument, 0, 'z'},
195 {"gzip", no_argument, 0, 'z'},
196 {"help", no_argument, &show_help, 1},
197 {"ignore-failed-read", no_argument, &ignore_failed_read_option, 1},
198 {"ignore-zeros", no_argument, 0, 'i'},
199 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
200 {"incremental", no_argument, 0, 'G'},
201 {"info-script", required_argument, 0, 'F'},
202 {"interactive", no_argument, 0, 'w'},
203 {"keep-old-files", no_argument, 0, 'k'},
204 {"label", required_argument, 0, 'V'},
205 {"list", no_argument, 0, 't'},
206 {"listed-incremental", required_argument, 0, 'g'},
207 {"mode", required_argument, 0, MODE_OPTION},
208 {"modification-time", no_argument, 0, OBSOLETE_TOUCH},
209 {"multi-volume", no_argument, 0, 'M'},
210 {"new-volume-script", required_argument, 0, 'F'},
211 {"newer", required_argument, 0, 'N'},
212 {"newer-mtime", required_argument, 0, NEWER_MTIME_OPTION},
213 {"null", no_argument, 0, NULL_OPTION},
214 {"no-recursion", no_argument, &recursion_option, 0},
215 {"no-same-owner", no_argument, &same_owner_option, -1},
216 {"no-same-permissions", no_argument, &same_permissions_option, -1},
217 {"numeric-owner", no_argument, &numeric_owner_option, 1},
218 {"old-archive", no_argument, 0, 'o'},
219 {"one-file-system", no_argument, 0, 'l'},
220 {"overwrite", no_argument, 0, OVERWRITE_OPTION},
221 {"owner", required_argument, 0, OWNER_OPTION},
222 {"portability", no_argument, 0, 'o'},
223 {"posix", no_argument, 0, POSIX_OPTION},
224 {"preserve", no_argument, 0, PRESERVE_OPTION},
225 {"preserve-order", no_argument, 0, 's'},
226 {"preserve-permissions", no_argument, 0, 'p'},
227 {"recursive-unlink", no_argument, &recursive_unlink_option, 1},
228 {"read-full-blocks", no_argument, 0, OBSOLETE_READ_FULL_RECORDS},
229 {"read-full-records", no_argument, 0, 'B'},
230 /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
231 {"record-number", no_argument, 0, OBSOLETE_BLOCK_NUMBER},
232 {"record-size", required_argument, 0, RECORD_SIZE_OPTION},
233 {"remove-files", no_argument, &remove_files_option, 1},
234 {"rsh-command", required_argument, 0, RSH_COMMAND_OPTION},
235 {"same-order", no_argument, 0, 's'},
236 {"same-owner", no_argument, &same_owner_option, 1},
237 {"same-permissions", no_argument, 0, 'p'},
238 {"show-omitted-dirs", no_argument, &show_omitted_dirs_option, 1},
239 {"sparse", no_argument, 0, 'S'},
240 {"starting-file", required_argument, 0, 'K'},
241 {"suffix", required_argument, 0, SUFFIX_OPTION},
242 {"tape-length", required_argument, 0, 'L'},
243 {"to-stdout", no_argument, 0, 'O'},
244 {"totals", no_argument, &totals_option, 1},
245 {"touch", no_argument, 0, 'm'},
246 {"uncompress", no_argument, 0, 'Z'},
247 {"ungzip", no_argument, 0, 'z'},
248 {"unlink-first", no_argument, 0, 'U'},
249 {"update", no_argument, 0, 'u'},
250 {"use-compress-program", required_argument, 0, USE_COMPRESS_PROGRAM_OPTION},
251 {"verbose", no_argument, 0, 'v'},
252 {"verify", no_argument, 0, 'W'},
253 {"version", no_argument, &show_version, 1},
254 {"version-control", required_argument, 0, OBSOLETE_VERSION_CONTROL},
255 {"volno-file", required_argument, 0, VOLNO_FILE_OPTION},
260 /* Print a usage message and exit with STATUS. */
264 if (status != TAREXIT_SUCCESS)
265 fprintf (stderr, _("Try `%s --help' for more information.\n"),
270 GNU `tar' saves many files together into a single tape or disk archive, and\n\
271 can restore individual files from the archive.\n"),
273 printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
276 %s -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
277 %s -tvf archive.tar # List all files in archive.tar verbosely.\n\
278 %s -xf archive.tar # Extract all files from archive.tar.\n"),
279 program_name, program_name, program_name, program_name);
282 If a long option shows an argument as mandatory, then it is mandatory\n\
283 for the equivalent short option also. Similarly for optional arguments.\n"),
287 Main operation mode:\n\
288 -t, --list list the contents of an archive\n\
289 -x, --extract, --get extract files from an archive\n\
290 -c, --create create a new archive\n\
291 -d, --diff, --compare find differences between archive and file system\n\
292 -r, --append append files to the end of an archive\n\
293 -u, --update only append files newer than copy in archive\n\
294 -A, --catenate append tar files to an archive\n\
295 --concatenate same as -A\n\
296 --delete delete from the archive (not on mag tapes!)\n"),
300 Operation modifiers:\n\
301 -W, --verify attempt to verify the archive after writing it\n\
302 --remove-files remove files after adding them to the archive\n\
303 -k, --keep-old-files don't replace existing files when extracting\n\
304 --overwrite overwrite existing files when extracting\n\
305 -U, --unlink-first remove each file prior to extracting over it\n\
306 --recursive-unlink empty hierarchies prior to extracting directory\n\
307 -S, --sparse handle sparse files efficiently\n\
308 -O, --to-stdout extract files to standard output\n\
309 -G, --incremental handle old GNU-format incremental backup\n\
310 -g, --listed-incremental=FILE\n\
311 handle new GNU-format incremental backup\n\
312 --ignore-failed-read do not exit with nonzero on unreadable files\n"),
316 Handling of file attributes:\n\
317 --owner=NAME force NAME as owner for added files\n\
318 --group=NAME force NAME as group for added files\n\
319 --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
320 --atime-preserve don't change access times on dumped files\n\
321 -m, --modification-time don't extract file modified time\n\
322 --same-owner try extracting files with the same ownership\n\
323 --no-same-owner extract files as yourself\n\
324 --numeric-owner always use numbers for user/group names\n\
325 -p, --same-permissions extract permissions information\n\
326 --no-same-permissions do not extract permissions information\n\
327 --preserve-permissions same as -p\n\
328 -s, --same-order sort names to extract to match archive\n\
329 --preserve-order same as -s\n\
330 --preserve same as both -p and -s\n"),
334 Device selection and switching:\n\
335 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
336 --force-local archive file is local even if has a colon\n\
337 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
338 -[0-7][lmh] specify drive and density\n\
339 -M, --multi-volume create/list/extract multi-volume archive\n\
340 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
341 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
342 --new-volume-script=FILE same as -F FILE\n\
343 --volno-file=FILE use/update the volume number in FILE\n"),
348 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
349 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
350 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
351 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
355 Archive format selection:\n\
356 -V, --label=NAME create archive with volume name NAME\n\
357 PATTERN at list/extract time, a globbing PATTERN\n\
358 -o, --old-archive, --portability write a V7 format archive\n\
359 --posix write a POSIX format archive\n\
360 -j, --bzip2 filter the archive through bzip2\n\
361 -z, --gzip, --ungzip filter the archive through gzip\n\
362 -Z, --compress, --uncompress filter the archive through compress\n\
363 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
367 Local file selection:\n\
368 -C, --directory=DIR change to directory DIR\n\
369 -T, --files-from=NAME get names to extract or create from file NAME\n\
370 --null -T reads null-terminated names, disable -C\n\
371 --exclude=PATTERN exclude files, given as a globbing PATTERN\n\
372 -X, --exclude-from=FILE exclude globbing patterns listed in FILE\n\
373 -P, --absolute-names don't strip leading `/'s from file names\n\
374 -h, --dereference dump instead the files symlinks point to\n\
375 --no-recursion avoid descending automatically in directories\n\
376 -l, --one-file-system stay in local file system when creating archive\n\
377 -K, --starting-file=NAME begin at file NAME in the archive\n"),
381 -N, --newer=DATE only store files newer than DATE\n\
382 --newer-mtime compare date and time when data changed only\n\
383 --after-date=DATE same as -N\n"),
387 --backup[=CONTROL] backup before removal, choose version control\n\
388 --suffix=SUFFIX backup before removal, override usual suffix\n"),
392 Informative output:\n\
393 --help print this help, then exit\n\
394 --version print tar program version number, then exit\n\
395 -v, --verbose verbosely list files processed\n\
396 --checkpoint print directory names while reading the archive\n\
397 --totals print total bytes written while creating archive\n\
398 -R, --block-number show block number within archive with each message\n\
399 -w, --interactive ask for confirmation for every action\n\
400 --confirmation same as -w\n"),
404 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
405 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
407 t, numbered make numbered backups\n\
408 nil, existing numbered if numbered backups exist, simple otherwise\n\
409 never, simple always make simple backups\n"),
413 GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
414 is set in the environment, GNU extensions are disallowed with `--posix'.\n\
415 Support for POSIX is only partially implemented, don't count on it yet.\n\
416 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; and FILE may be a file\n\
417 or a device. *This* `tar' defaults to `-f%s -b%d'.\n"),
418 DEFAULT_ARCHIVE, DEFAULT_BLOCKING);
419 fputs (_("\nReport bugs to <bug-tar@gnu.org>.\n"), stdout);
424 /* Parse the options for tar. */
426 /* Available option letters are DEHIJQY and aenqy. Some are reserved:
428 e exit immediately with a nonzero exit status if unexpected errors occur
429 E use extended headers (draft POSIX headers, that is)
430 I same as T (for compatibility with Solaris tar)
431 n the archive is quickly seekable, so don't worry about random seeks
432 q stop after extracting the first occurrence of the named file
433 y per-file gzip compression
434 Y per-block gzip compression */
436 #define OPTION_STRING \
437 "-01234567ABC:F:GK:L:MN:OPRST:UV:WX:Zb:cdf:g:hijklmoprstuvwxz"
440 set_subcommand_option (enum subcommand subcommand)
442 if (subcommand_option != UNKNOWN_SUBCOMMAND
443 && subcommand_option != subcommand)
445 _("You may not specify more than one `-Acdtrux' option")));
447 subcommand_option = subcommand;
451 set_use_compress_program_option (const char *string)
453 if (use_compress_program_option && strcmp (use_compress_program_option, string) != 0)
454 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
456 use_compress_program_option = string;
459 /* Ignore DUMMY (which will always be null in practice), and add
460 PATTERN to the proper set of patterns to be excluded -- either
461 patterns with slashes, or patterns without. */
463 add_filtered_exclude (struct exclude *dummy, char const *pattern)
465 add_exclude ((strchr (pattern, '/')
466 ? excluded_with_slash
467 : excluded_without_slash),
472 decode_options (int argc, char **argv)
474 int optchar; /* option letter */
475 int input_files; /* number of input files */
476 const char *backup_suffix_string;
477 const char *version_control_string = 0;
479 /* Set some default option values. */
481 subcommand_option = UNKNOWN_SUBCOMMAND;
482 archive_format = DEFAULT_FORMAT;
483 blocking_factor = DEFAULT_BLOCKING;
484 record_size = DEFAULT_BLOCKING * BLOCKSIZE;
485 excluded_with_slash = new_exclude ();
486 excluded_without_slash = new_exclude ();
487 newer_mtime_option = TYPE_MINIMUM (time_t);
488 recursion_option = FNM_LEADING_DIR;
493 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
495 /* Convert old-style tar call by exploding option element and rearranging
496 options accordingly. */
498 if (argc > 1 && argv[1][0] != '-')
500 int new_argc; /* argc value for rearranged arguments */
501 char **new_argv; /* argv value for rearranged arguments */
502 char *const *in; /* cursor into original argv */
503 char **out; /* cursor into rearranged argv */
504 const char *letter; /* cursor into old option letters */
505 char buffer[3]; /* constructed option buffer */
506 const char *cursor; /* cursor in OPTION_STRING */
508 /* Initialize a constructed option. */
513 /* Allocate a new argument array, and copy program name in it. */
515 new_argc = argc - 1 + strlen (argv[1]);
516 new_argv = xmalloc (new_argc * sizeof (char *));
521 /* Copy each old letter option as a separate option, and have the
522 corresponding argument moved next to it. */
524 for (letter = *in++; *letter; letter++)
527 *out++ = xstrdup (buffer);
528 cursor = strchr (OPTION_STRING, *letter);
529 if (cursor && cursor[1] == ':')
531 if (in < argv + argc)
534 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
539 /* Copy all remaining options. */
541 while (in < argv + argc)
544 /* Replace the old option list by the new one. */
550 /* Parse all options and non-options as they appear. */
554 prepend_default_options (getenv ("TAR_OPTIONS"), &argc, &argv);
556 while (optchar = getopt_long (argc, argv, OPTION_STRING, long_options, 0),
561 usage (TAREXIT_FAILURE);
567 /* File name or non-parsed option, because of RETURN_IN_ORDER
568 ordering triggered by the leading dash in OPTION_STRING. */
575 set_subcommand_option (CAT_SUBCOMMAND);
578 case OBSOLETE_BLOCK_COMPRESS:
579 WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
582 case OBSOLETE_BLOCKING_FACTOR:
583 WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
589 if (! (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
590 && u == (blocking_factor = u)
591 && 0 < blocking_factor
592 && u == (record_size = u * BLOCKSIZE) / BLOCKSIZE))
593 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
594 _("Invalid blocking factor")));
598 case OBSOLETE_READ_FULL_RECORDS:
600 _("Obsolete option name replaced by --read-full-records")));
604 /* Try to reblock input records. For reading 4.2BSD pipes. */
606 /* It would surely make sense to exchange -B and -R, but it seems
607 that -B has been used for a long while in Sun tar ans most
608 BSD-derived systems. This is a consequence of the block/record
609 terminology confusion. */
611 read_full_records_option = 1;
615 set_subcommand_option (CREATE_SUBCOMMAND);
624 set_subcommand_option (DIFF_SUBCOMMAND);
628 if (archive_names == allocated_archive_names)
630 allocated_archive_names *= 2;
632 xrealloc (archive_name_array,
633 sizeof (const char *) * allocated_archive_names);
635 archive_name_array[archive_names++] = optarg;
639 /* Since -F is only useful with -M, make it implied. Run this
640 script at the end of each tape. */
642 info_script_option = optarg;
643 multi_volume_option = 1;
647 listed_incremental_option = optarg;
648 after_date_option = 1;
652 /* We are making an incremental dump (FIXME: are we?); save
653 directories at the beginning of the archive, and include in each
654 directory its contents. */
656 incremental_option = 1;
660 /* Follow symbolic links. */
662 dereference_option = 1;
666 /* Ignore zero blocks (eofs). This can't be the default,
667 because Unix tar writes two blocks of zeros, then pads out
668 the record with garbage. */
670 ignore_zeros_option = 1;
674 set_use_compress_program_option ("bzip2");
678 /* Don't replace existing files. */
679 old_files_option = KEEP_OLD_FILES;
683 starting_file_option = 1;
688 /* When dumping directories, don't dump files/subdirectories
689 that are on other filesystems. */
691 one_file_system_option = 1;
697 if (xstrtoumax (optarg, 0, 10, &u, "") != LONGINT_OK)
698 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
699 _("Invalid tape length")));
700 tape_length_option = 1024 * (tarlong) u;
701 multi_volume_option = 1;
706 WARN ((0, 0, _("Obsolete option name replaced by --touch")));
714 /* Make multivolume archive: when we can't write any more into
715 the archive, re-open it, and continue writing. */
717 multi_volume_option = 1;
722 after_date_option = 1;
725 case NEWER_MTIME_OPTION:
726 if (newer_mtime_option != TYPE_MINIMUM (time_t))
727 USAGE_ERROR ((0, 0, _("More than one threshold date")));
729 if (FILESYSTEM_PREFIX_LEN (optarg) != 0
734 if (deref_stat (dereference_option, optarg, &st) != 0)
737 USAGE_ERROR ((0, 0, _("Date file not found")));
739 newer_mtime_option = st.st_mtime;
743 newer_mtime_option = get_date (optarg, 0);
744 if (newer_mtime_option == (time_t) -1)
745 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
746 tartime (newer_mtime_option), quote (optarg)));
750 #endif /* not MSDOS */
753 if (archive_format == DEFAULT_FORMAT)
754 archive_format = V7_FORMAT;
755 else if (archive_format != V7_FORMAT)
756 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
760 to_stdout_option = 1;
764 same_permissions_option = 1;
767 case OBSOLETE_ABSOLUTE_NAMES:
768 WARN ((0, 0, _("Obsolete option name replaced by --absolute-names")));
772 absolute_names_option = 1;
776 set_subcommand_option (APPEND_SUBCOMMAND);
779 case OBSOLETE_BLOCK_NUMBER:
780 WARN ((0, 0, _("Obsolete option name replaced by --block-number")));
784 /* Print block numbers for debugging bad tar archives. */
786 /* It would surely make sense to exchange -B and -R, but it seems
787 that -B has been used for a long while in Sun tar ans most
788 BSD-derived systems. This is a consequence of the block/record
789 terminology confusion. */
791 block_number_option = 1;
795 /* Names to extr are sorted. */
797 same_order_option = 1;
805 set_subcommand_option (LIST_SUBCOMMAND);
810 files_from_option = optarg;
814 set_subcommand_option (UPDATE_SUBCOMMAND);
818 old_files_option = UNLINK_FIRST_OLD_FILES;
826 volume_label_option = optarg;
830 interactive_option = 1;
838 set_subcommand_option (EXTRACT_SUBCOMMAND);
842 if (add_exclude_file (add_filtered_exclude, 0, optarg, '\n') != 0)
845 FATAL_ERROR ((0, e, "%s", quotearg_colon (optarg)));
850 set_use_compress_program_option ("gzip");
854 set_use_compress_program_option ("compress");
857 case OBSOLETE_VERSION_CONTROL:
858 WARN ((0, 0, _("Obsolete option name replaced by --backup")));
864 version_control_string = optarg;
868 set_subcommand_option (DELETE_SUBCOMMAND);
872 add_filtered_exclude (0, optarg);
876 if (! (strlen (optarg) < GNAME_FIELD_SIZE
877 && gname_to_gid (optarg, &group_option)))
880 if (xstrtoumax (optarg, 0, 10, &g, "") == LONGINT_OK
884 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
885 _("%s: Invalid group")));
891 = mode_compile (optarg,
892 MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
893 if (mode_option == MODE_INVALID)
894 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
895 if (mode_option == MODE_MEMORY_EXHAUSTED)
900 filename_terminator = '\0';
903 case OVERWRITE_OPTION:
904 old_files_option = OVERWRITE_OLD_FILES;
908 if (! (strlen (optarg) < UNAME_FIELD_SIZE
909 && uname_to_uid (optarg, &owner_option)))
912 if (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
916 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
917 _("Invalid owner")));
922 #if OLDGNU_COMPATIBILITY
923 if (archive_format == DEFAULT_FORMAT)
924 archive_format = GNU_FORMAT;
925 else if (archive_format != GNU_FORMAT)
926 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
928 if (archive_format == DEFAULT_FORMAT)
929 archive_format = POSIX_FORMAT;
930 else if (archive_format != POSIX_FORMAT)
931 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
935 case PRESERVE_OPTION:
936 same_permissions_option = 1;
937 same_order_option = 1;
940 case RECORD_SIZE_OPTION:
943 if (! (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
945 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
946 _("Invalid record size")));
948 if (record_size % BLOCKSIZE != 0)
949 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
951 blocking_factor = record_size / BLOCKSIZE;
955 case RSH_COMMAND_OPTION:
956 rsh_command_option = optarg;
961 backup_suffix_string = optarg;
964 case VOLNO_FILE_OPTION:
965 volno_file_option = optarg;
968 case USE_COMPRESS_PROGRAM_OPTION:
969 set_use_compress_program_option (optarg);
983 int device = optchar - '0';
985 static char buf[sizeof DEVICE_PREFIX + 10];
988 density = getopt_long (argc, argv, "lmh", 0, 0);
989 strcpy (buf, DEVICE_PREFIX);
990 cursor = buf + strlen (buf);
992 #ifdef DENSITY_LETTER
994 sprintf (cursor, "%d%c", device, density);
996 #else /* not DENSITY_LETTER */
1023 usage (TAREXIT_FAILURE);
1025 sprintf (cursor, "%d", device);
1027 #endif /* not DENSITY_LETTER */
1029 if (archive_names == allocated_archive_names)
1031 allocated_archive_names *= 2;
1032 archive_name_array =
1033 xrealloc (archive_name_array,
1034 sizeof (const char *) * allocated_archive_names);
1036 archive_name_array[archive_names++] = buf;
1038 /* FIXME: How comes this works for many archives when buf is
1043 #else /* not DEVICE_PREFIX */
1046 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1048 #endif /* not DEVICE_PREFIX */
1051 /* Handle operands after any "--" argument. */
1052 for (; optind < argc; optind++)
1054 name_add (argv[optind]);
1058 /* Process trivial options. */
1062 printf ("tar (GNU %s) %s\n%s\n%s\n%s\n", PACKAGE, VERSION,
1063 "Copyright 2001 Free Software Foundation, Inc.",
1065 This program comes with NO WARRANTY, to the extent permitted by law.\n\
1066 You may redistribute it under the terms of the GNU General Public License;\n\
1067 see the file named COPYING for details."),
1068 _("Written by John Gilmore and Jay Fenlason."));
1069 exit (TAREXIT_SUCCESS);
1073 usage (TAREXIT_SUCCESS);
1075 /* Derive option values and check option consistency. */
1077 if (archive_format == DEFAULT_FORMAT)
1079 #if OLDGNU_COMPATIBILITY
1080 archive_format = OLDGNU_FORMAT;
1082 archive_format = GNU_FORMAT;
1086 if (archive_format == GNU_FORMAT && getenv ("POSIXLY_CORRECT"))
1087 archive_format = POSIX_FORMAT;
1089 if ((volume_label_option
1090 || incremental_option || multi_volume_option || sparse_option)
1091 && archive_format != OLDGNU_FORMAT && archive_format != GNU_FORMAT)
1093 _("GNU features wanted on incompatible archive format")));
1095 if (archive_names == 0)
1097 /* If no archive file name given, try TAPE from the environment, or
1098 else, DEFAULT_ARCHIVE from the configuration process. */
1101 archive_name_array[0] = getenv ("TAPE");
1102 if (! archive_name_array[0])
1103 archive_name_array[0] = DEFAULT_ARCHIVE;
1106 /* Allow multiple archives only with `-M'. */
1108 if (archive_names > 1 && !multi_volume_option)
1110 _("Multiple archive files requires `-M' option")));
1112 if (listed_incremental_option
1113 && newer_mtime_option != TYPE_MINIMUM (time_t))
1115 _("Cannot combine --listed-incremental with --newer")));
1117 if (volume_label_option)
1119 size_t volume_label_max_len =
1120 (sizeof current_header->header.name
1121 - 1 /* for trailing '\0' */
1122 - (multi_volume_option
1123 ? (sizeof " Volume "
1124 - 1 /* for null at end of " Volume " */
1125 + INT_STRLEN_BOUND (int) /* for volume number */
1126 - 1 /* for sign, as 0 <= volno */)
1128 if (volume_label_max_len < strlen (volume_label_option))
1130 _("%s: Volume label is too long (limit is %lu bytes)"),
1131 quotearg_colon (volume_label_option),
1132 (unsigned long) volume_label_max_len));
1135 /* If ready to unlink hierarchies, so we are for simpler files. */
1136 if (recursive_unlink_option)
1137 old_files_option = UNLINK_FIRST_OLD_FILES;
1139 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1140 explicit or implied, is used correctly. */
1142 switch (subcommand_option)
1144 case CREATE_SUBCOMMAND:
1145 if (input_files == 0 && !files_from_option)
1147 _("Cowardly refusing to create an empty archive")));
1150 case EXTRACT_SUBCOMMAND:
1151 case LIST_SUBCOMMAND:
1152 case DIFF_SUBCOMMAND:
1153 for (archive_name_cursor = archive_name_array;
1154 archive_name_cursor < archive_name_array + archive_names;
1155 archive_name_cursor++)
1156 if (!strcmp (*archive_name_cursor, "-"))
1157 request_stdin ("-f");
1160 case CAT_SUBCOMMAND:
1161 case UPDATE_SUBCOMMAND:
1162 case APPEND_SUBCOMMAND:
1163 for (archive_name_cursor = archive_name_array;
1164 archive_name_cursor < archive_name_array + archive_names;
1165 archive_name_cursor++)
1166 if (!strcmp (*archive_name_cursor, "-"))
1168 _("Options `-Aru' are incompatible with `-f -'")));
1174 archive_name_cursor = archive_name_array;
1176 /* Prepare for generating backup names. */
1178 if (backup_suffix_string)
1179 simple_backup_suffix = xstrdup (backup_suffix_string);
1182 backup_type = xget_version ("--backup", version_control_string);
1187 /* Main routine for tar. */
1189 main (int argc, char **argv)
1191 #if HAVE_CLOCK_GETTIME
1192 if (clock_gettime (CLOCK_REALTIME, &start_timespec) != 0)
1194 start_time = time (0);
1195 program_name = argv[0];
1196 setlocale (LC_ALL, "");
1197 bindtextdomain (PACKAGE, LOCALEDIR);
1198 textdomain (PACKAGE);
1200 exit_status = TAREXIT_SUCCESS;
1201 filename_terminator = '\n';
1202 set_quoting_style (0, escape_quoting_style);
1204 /* Pre-allocate a few structures. */
1206 allocated_archive_names = 10;
1207 archive_name_array =
1208 xmalloc (sizeof (const char *) * allocated_archive_names);
1212 /* System V fork+wait does not work if SIGCHLD is ignored. */
1213 signal (SIGCHLD, SIG_DFL);
1218 /* Decode options. */
1220 decode_options (argc, argv);
1221 name_init (argc, argv);
1223 /* Main command execution. */
1225 if (volno_file_option)
1226 init_volume_number ();
1228 switch (subcommand_option)
1230 case UNKNOWN_SUBCOMMAND:
1232 _("You must specify one of the `-Acdtrux' options")));
1234 case CAT_SUBCOMMAND:
1235 case UPDATE_SUBCOMMAND:
1236 case APPEND_SUBCOMMAND:
1240 case DELETE_SUBCOMMAND:
1241 delete_archive_members ();
1244 case CREATE_SUBCOMMAND:
1249 print_total_written ();
1252 case EXTRACT_SUBCOMMAND:
1254 read_and (extract_archive);
1258 case LIST_SUBCOMMAND:
1259 read_and (list_archive);
1262 case DIFF_SUBCOMMAND:
1264 read_and (diff_archive);
1268 if (volno_file_option)
1269 closeout_volume_number ();
1271 /* Dispose of allocated memory, and return. */
1273 free (archive_name_array);
1276 if (stdlis == stdout && (ferror (stdout) || fclose (stdout) != 0))
1277 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1278 if (exit_status == TAREXIT_FAILURE)
1279 error (0, 0, _("Error exit delayed from previous errors"));