1 /* A tar (tape archiver) program.
3 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001
4 Free Software Foundation, Inc.
6 Written by John Gilmore, starting 1985-08-25.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* $FreeBSD: src/contrib/tar/src/tar.c,v 1.2.2.1 2002/07/14 13:19:44 sobomax Exp $ */
30 #if ! defined SIGCHLD && defined SIGCLD
31 # define SIGCHLD SIGCLD
34 /* The following causes "common.h" to produce definitions of all the global
35 variables, rather than just "extern" declarations of them. GNU tar does
36 depend on the system loader to preset all GLOBAL variables to neutral (or
37 zero) values; explicit initialization is usually not done. */
41 #include <print-copyr.h>
42 #include <localedir.h>
49 /* Local declarations. */
51 #ifndef DEFAULT_ARCHIVE
52 # define DEFAULT_ARCHIVE "tar.out"
55 #ifndef DEFAULT_BLOCKING
56 # define DEFAULT_BLOCKING 20
59 static void usage PARAMS ((int)) __attribute__ ((noreturn));
63 /* Name of option using stdin. */
64 static const char *stdin_used_by;
66 /* Doesn't return if stdin already requested. */
68 request_stdin (const char *option)
71 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
72 stdin_used_by, option));
74 stdin_used_by = option;
77 /* Returns true if and only if the user typed 'y' or 'Y'. */
79 confirm (const char *message_action, const char *message_name)
81 static FILE *confirm_file;
82 static int confirm_file_EOF;
86 if (archive == 0 || stdin_used_by)
88 confirm_file = fopen (TTY_NAME, "r");
90 open_fatal (TTY_NAME);
99 fprintf (stdlis, "%s %s?", message_action, quote (message_name));
103 int reply = confirm_file_EOF ? EOF : getc (confirm_file);
106 for (character = reply;
108 character = getc (confirm_file))
109 if (character == EOF)
111 confirm_file_EOF = 1;
112 fputc ('\n', stdlis);
116 return reply == 'y' || reply == 'Y';
122 /* For long options that unconditionally set a single flag, we have getopt
123 do it. For the others, we share the code for the equivalent short
124 named option, the name of which is stored in the otherwise-unused `val'
125 field of the `struct option'; for long options that have no equivalent
126 short option, we use non-characters as pseudo short options,
127 starting at CHAR_MAX + 1 and going upwards. */
131 ANCHORED_OPTION = CHAR_MAX + 1,
140 NO_IGNORE_CASE_OPTION,
142 NO_WILDCARDS_MATCH_SLASH_OPTION,
145 OVERWRITE_DIR_OPTION,
152 USE_COMPRESS_PROGRAM_OPTION,
155 WILDCARDS_MATCH_SLASH_OPTION,
157 /* Some cleanup is being made in GNU tar long options. Using old names is
158 allowed for a while, but will also send a warning to stderr. Take old
159 names out in 1.14, or in summer 1997, whichever happens last. */
161 OBSOLETE_ABSOLUTE_NAMES,
162 OBSOLETE_BLOCK_COMPRESS,
163 OBSOLETE_BLOCKING_FACTOR,
164 OBSOLETE_BLOCK_NUMBER,
165 OBSOLETE_READ_FULL_RECORDS,
167 OBSOLETE_VERSION_CONTROL
170 /* If nonzero, display usage information and exit. */
171 static int show_help;
173 /* If nonzero, print the version on standard output and exit. */
174 static int show_version;
176 static struct option long_options[] =
178 {"absolute-names", no_argument, 0, 'P'},
179 {"absolute-paths", no_argument, 0, OBSOLETE_ABSOLUTE_NAMES},
180 {"after-date", required_argument, 0, 'N'},
181 {"anchored", no_argument, 0, ANCHORED_OPTION},
182 {"append", no_argument, 0, 'r'},
183 {"atime-preserve", no_argument, &atime_preserve_option, 1},
184 {"backup", optional_argument, 0, BACKUP_OPTION},
185 {"block-compress", no_argument, 0, OBSOLETE_BLOCK_COMPRESS},
186 {"block-number", no_argument, 0, 'R'},
187 {"block-size", required_argument, 0, OBSOLETE_BLOCKING_FACTOR},
188 {"blocking-factor", required_argument, 0, 'b'},
189 {"bzip", no_argument, 0, 'j'},
190 {"bzip2", no_argument, 0, 'j'},
191 {"bunzip2", no_argument, 0, 'j'},
192 {"catenate", no_argument, 0, 'A'},
193 {"checkpoint", no_argument, &checkpoint_option, 1},
194 {"compare", no_argument, 0, 'd'},
195 {"compress", no_argument, 0, 'Z'},
196 {"concatenate", no_argument, 0, 'A'},
197 {"confirmation", no_argument, 0, 'w'},
198 /* FIXME: --selective as a synonym for --confirmation? */
199 {"create", no_argument, 0, 'c'},
200 {"delete", no_argument, 0, DELETE_OPTION},
201 {"dereference", no_argument, 0, 'h'},
202 {"diff", no_argument, 0, 'd'},
203 {"directory", required_argument, 0, 'C'},
204 {"exclude", required_argument, 0, EXCLUDE_OPTION},
205 {"exclude-from", required_argument, 0, 'X'},
206 {"extract", no_argument, 0, 'x'},
207 {"fast-read", no_argument, &fast_read_option, 1},
208 {"file", required_argument, 0, 'f'},
209 {"files-from", required_argument, 0, 'T'},
210 {"force-local", no_argument, &force_local_option, 1},
211 {"get", no_argument, 0, 'x'},
212 {"group", required_argument, 0, GROUP_OPTION},
213 {"gunzip", no_argument, 0, 'z'},
214 {"gzip", no_argument, 0, 'z'},
215 {"help", no_argument, &show_help, 1},
216 {"ignore-case", no_argument, 0, IGNORE_CASE_OPTION},
217 {"ignore-failed-read", no_argument, &ignore_failed_read_option, 1},
218 {"ignore-zeros", no_argument, 0, 'i'},
219 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
220 {"incremental", no_argument, 0, 'G'},
221 {"info-script", required_argument, 0, 'F'},
222 {"interactive", no_argument, 0, 'w'},
223 {"keep-old-files", no_argument, 0, 'k'},
224 {"label", required_argument, 0, 'V'},
225 {"list", no_argument, 0, 't'},
226 {"listed-incremental", required_argument, 0, 'g'},
227 {"mode", required_argument, 0, MODE_OPTION},
228 {"modification-time", no_argument, 0, OBSOLETE_TOUCH},
229 {"multi-volume", no_argument, 0, 'M'},
230 {"new-volume-script", required_argument, 0, 'F'},
231 {"newer", required_argument, 0, 'N'},
232 {"newer-mtime", required_argument, 0, NEWER_MTIME_OPTION},
233 {"null", no_argument, 0, NULL_OPTION},
234 {"no-anchored", no_argument, 0, NO_ANCHORED_OPTION},
235 {"no-ignore-case", no_argument, 0, NO_IGNORE_CASE_OPTION},
236 {"no-wildcards", no_argument, 0, NO_WILDCARDS_OPTION},
237 {"no-wildcards-match-slash", no_argument, 0, NO_WILDCARDS_MATCH_SLASH_OPTION},
238 {"norecurse", no_argument, &recursion_option, 0},
239 {"no-recursion", no_argument, &recursion_option, 0},
240 {"no-same-owner", no_argument, &same_owner_option, -1},
241 {"no-same-permissions", no_argument, &same_permissions_option, -1},
242 {"numeric-owner", no_argument, &numeric_owner_option, 1},
243 {"old-archive", no_argument, 0, 'o'},
244 {"one-file-system", no_argument, 0, 'l'},
245 {"overwrite", no_argument, 0, OVERWRITE_OPTION},
246 {"overwrite-dir", no_argument, 0, OVERWRITE_DIR_OPTION},
247 {"owner", required_argument, 0, OWNER_OPTION},
248 {"portability", no_argument, 0, 'o'},
249 {"posix", no_argument, 0, POSIX_OPTION},
250 {"preserve", no_argument, 0, PRESERVE_OPTION},
251 {"preserve-order", no_argument, 0, 's'},
252 {"preserve-permissions", no_argument, 0, 'p'},
253 {"recursion", no_argument, &recursion_option, FNM_LEADING_DIR},
254 {"recursive-unlink", no_argument, &recursive_unlink_option, 1},
255 {"read-full-blocks", no_argument, 0, OBSOLETE_READ_FULL_RECORDS},
256 {"read-full-records", no_argument, 0, 'B'},
257 /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
258 {"record-number", no_argument, 0, OBSOLETE_BLOCK_NUMBER},
259 {"record-size", required_argument, 0, RECORD_SIZE_OPTION},
260 {"remove-files", no_argument, &remove_files_option, 1},
261 {"rsh-command", required_argument, 0, RSH_COMMAND_OPTION},
262 {"same-order", no_argument, 0, 's'},
263 {"same-owner", no_argument, &same_owner_option, 1},
264 {"same-permissions", no_argument, 0, 'p'},
265 {"show-omitted-dirs", no_argument, &show_omitted_dirs_option, 1},
266 {"sparse", no_argument, 0, 'S'},
267 {"starting-file", required_argument, 0, 'K'},
268 {"suffix", required_argument, 0, SUFFIX_OPTION},
269 {"tape-length", required_argument, 0, 'L'},
270 {"to-stdout", no_argument, 0, 'O'},
271 {"totals", no_argument, &totals_option, 1},
272 {"touch", no_argument, 0, 'm'},
273 {"uncompress", no_argument, 0, 'Z'},
274 {"ungzip", no_argument, 0, 'z'},
275 {"unlink", no_argument, 0, 'U'},
276 {"unlink-first", no_argument, 0, 'U'},
277 {"update", no_argument, 0, 'u'},
278 {"use-compress-program", required_argument, 0, USE_COMPRESS_PROGRAM_OPTION},
279 {"verbose", no_argument, 0, 'v'},
280 {"verify", no_argument, 0, 'W'},
281 {"version", no_argument, &show_version, 1},
282 {"version-control", required_argument, 0, OBSOLETE_VERSION_CONTROL},
283 {"volno-file", required_argument, 0, VOLNO_FILE_OPTION},
284 {"wildcards", no_argument, 0, WILDCARDS_OPTION},
285 {"wildcards-match-slash", no_argument, 0, WILDCARDS_MATCH_SLASH_OPTION},
290 /* Print a usage message and exit with STATUS. */
294 if (status != TAREXIT_SUCCESS)
295 fprintf (stderr, _("Try `%s --help' for more information.\n"),
300 GNU `tar' saves many files together into a single tape or disk archive, and\n\
301 can restore individual files from the archive.\n"),
303 printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
306 %s -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
307 %s -tvf archive.tar # List all files in archive.tar verbosely.\n\
308 %s -xf archive.tar # Extract all files from archive.tar.\n"),
309 program_name, program_name, program_name, program_name);
312 If a long option shows an argument as mandatory, then it is mandatory\n\
313 for the equivalent short option also. Similarly for optional arguments.\n"),
317 Main operation mode:\n\
318 -t, --list list the contents of an archive\n\
319 -x, --extract, --get extract files from an archive\n\
320 -c, --create create a new archive\n\
321 -d, --diff, --compare find differences between archive and file system\n\
322 -r, --append append files to the end of an archive\n\
323 -u, --update only append files newer than copy in archive\n\
324 -A, --catenate append tar files to an archive\n\
325 --concatenate same as -A\n\
326 --delete delete from the archive (not on mag tapes!)\n"),
330 Operation modifiers:\n\
331 -W, --verify attempt to verify the archive after writing it\n\
332 --remove-files remove files after adding them to the archive\n\
333 -k, --keep-old-files don't replace existing files when extracting\n\
334 --overwrite overwrite existing files when extracting\n\
335 --overwrite-dir overwrite directory metadata when extracting\n\
337 --unlink-first remove each file prior to extracting over it\n\
338 --recursive-unlink empty hierarchies prior to extracting directory\n\
339 -S, --sparse handle sparse files efficiently\n\
340 -O, --to-stdout extract files to standard output\n\
341 -G, --incremental handle old GNU-format incremental backup\n\
342 -g, --listed-incremental=FILE\n\
343 handle new GNU-format incremental backup\n\
344 --ignore-failed-read do not exit with nonzero on unreadable files\n\
345 --fast-read stop after desired names in archive have been found\n"),
349 Handling of file attributes:\n\
350 --owner=NAME force NAME as owner for added files\n\
351 --group=NAME force NAME as group for added files\n\
352 --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
353 --atime-preserve don't change access times on dumped files\n\
354 -m, --modification-time don't extract file modified time\n\
355 --same-owner try extracting files with the same ownership\n\
356 --show-omitted-dirs show omitted directories while processing the\n\
358 --no-same-owner extract files as yourself\n\
359 --numeric-owner always use numbers for user/group names\n\
360 -p, --same-permissions extract permissions information\n\
361 --no-same-permissions do not extract permissions information\n\
362 --preserve-permissions same as -p\n\
363 -s, --same-order sort names to extract to match archive\n\
364 --preserve-order same as -s\n\
365 --preserve same as both -p and -s\n"),
369 Device selection and switching:\n\
370 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
371 --force-local archive file is local even if it has a colon\n\
372 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
373 -[0-7][lmh] specify drive and density\n\
374 -M, --multi-volume create/list/extract multi-volume archive\n\
375 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
376 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
377 --new-volume-script=FILE same as -F FILE\n\
378 --volno-file=FILE use/update the volume number in FILE\n"),
383 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
384 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
385 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
386 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
390 Archive format selection:\n\
391 -V, --label=NAME create archive with volume name NAME\n\
392 PATTERN at list/extract time, a globbing PATTERN\n\
393 -o, --old-archive, --portability write a V7 format archive\n\
394 --posix write a POSIX format archive\n\
395 -j, -y, --bzip, --bzip2, --bunzip2 filter the archive through bzip2\n\
396 -z, --gzip, --ungzip filter the archive through gzip\n\
397 -Z, --compress, --uncompress filter the archive through compress\n\
398 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
402 Local file selection:\n\
403 -C, --directory=DIR change to directory DIR\n\
404 -T, -I, --files-from=NAME get names to extract or create from file NAME\n\
405 --null -T reads null-terminated names, disable -C\n\
406 --exclude=PATTERN exclude files, given as a a globbing PATTERN\n\
407 -X, --exclude-from=FILE exclude patterns listed in FILE\n\
408 --anchored exclude patterns match file name start (default)\n\
409 --no-anchored exclude patterns match after any /\n\
410 --ignore-case exclusion ignores case\n\
411 --no-ignore-case exclusion is case sensitive (default)\n\
412 --wildcards exclude patterns use wildcards (default)\n\
413 --no-wildcards exclude patterns are plain strings\n\
414 --wildcards-match-slash exclude pattern wildcards match '/' (default)\n\
415 --no-wildcards-match-slash exclude pattern wildcards do not match '/'\n\
416 -P, --absolute-names don't strip leading `/'s from file names\n\
417 -h, --dereference dump instead the files symlinks point to\n\
419 --no-recursion avoid descending automatically in directories\n\
420 -l, --one-file-system stay in local file system when creating archive\n\
421 -K, --starting-file=NAME begin at file NAME in the archive\n"),
425 -N, --newer=DATE only store files with creation time newer than\n\
427 --newer-mtime=DATE only store files with modification time newer\n\
429 --after-date=DATE same as -N\n"),
433 --backup[=CONTROL] backup before removal, choose version control\n\
434 --suffix=SUFFIX backup before removal, override usual suffix\n"),
438 Informative output:\n\
439 --help print this help, then exit\n\
440 --version print tar program version number, then exit\n\
441 -v, --verbose verbosely list files processed\n\
442 --checkpoint print number of buffer reads/writes\n\
443 --totals print total bytes written while creating archive\n\
444 -R, --block-number show block number within archive with each message\n\
445 -w, --interactive ask for confirmation for every action\n\
446 --confirmation same as -w\n"),
450 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
451 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
453 t, numbered make numbered backups\n\
454 nil, existing numbered if numbered backups exist, simple otherwise\n\
455 never, simple always make simple backups\n"),
459 GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
460 is set in the environment, GNU extensions are disallowed with `--posix'.\n\
461 Support for POSIX is only partially implemented, don't count on it yet.\n\
462 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; DATE may be a textual date\n\
463 or a file name starting with `/' or `.', in which case the file's date is used.\n\
464 *This* `tar' defaults to `-f%s -b%d'.\n"),
465 DEFAULT_ARCHIVE, DEFAULT_BLOCKING);
466 fputs (_("\nReport bugs to <bug-tar@gnu.org>.\n"), stdout);
471 /* Parse the options for tar. */
473 /* Available option letters are DEHIJQY and aenqy. Some are reserved:
475 e exit immediately with a nonzero exit status if unexpected errors occur
476 E use extended headers (draft POSIX headers, that is)
477 I same as T (for compatibility with Solaris tar)
478 n the archive is quickly seekable, so don't worry about random seeks
479 q stop after extracting the first occurrence of the named file
480 y per-file gzip compression
481 Y per-block gzip compression */
483 #define OPTION_STRING \
484 "-01234567ABC:F:GI:K:L:MnN:OPRST:UV:WX:Zb:cdf:g:hijklmoprstuvwxyz"
487 set_subcommand_option (enum subcommand subcommand)
489 if (subcommand_option != UNKNOWN_SUBCOMMAND
490 && subcommand_option != subcommand)
492 _("You may not specify more than one `-Acdtrux' option")));
494 subcommand_option = subcommand;
498 set_use_compress_program_option (const char *string)
500 if (use_compress_program_option && strcmp (use_compress_program_option, string) != 0)
501 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
503 use_compress_program_option = string;
507 decode_options (int argc, char **argv)
509 int optchar; /* option letter */
510 int input_files; /* number of input files */
511 const char *backup_suffix_string;
512 const char *version_control_string = 0;
513 int exclude_options = EXCLUDE_WILDCARDS;
515 /* Set some default option values. */
517 subcommand_option = UNKNOWN_SUBCOMMAND;
518 archive_format = DEFAULT_FORMAT;
519 blocking_factor = DEFAULT_BLOCKING;
520 record_size = DEFAULT_BLOCKING * BLOCKSIZE;
521 excluded = new_exclude ();
522 newer_mtime_option = TYPE_MINIMUM (time_t);
523 recursion_option = FNM_LEADING_DIR;
529 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
531 /* Convert old-style tar call by exploding option element and rearranging
532 options accordingly. */
534 if (argc > 1 && argv[1][0] != '-')
536 int new_argc; /* argc value for rearranged arguments */
537 char **new_argv; /* argv value for rearranged arguments */
538 char *const *in; /* cursor into original argv */
539 char **out; /* cursor into rearranged argv */
540 const char *letter; /* cursor into old option letters */
541 char buffer[3]; /* constructed option buffer */
542 const char *cursor; /* cursor in OPTION_STRING */
544 /* Initialize a constructed option. */
549 /* Allocate a new argument array, and copy program name in it. */
551 new_argc = argc - 1 + strlen (argv[1]);
552 new_argv = xmalloc (new_argc * sizeof (char *));
557 /* Copy each old letter option as a separate option, and have the
558 corresponding argument moved next to it. */
560 for (letter = *in++; *letter; letter++)
563 *out++ = xstrdup (buffer);
564 cursor = strchr (OPTION_STRING, *letter);
565 if (cursor && cursor[1] == ':')
567 if (in < argv + argc)
570 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
575 /* Copy all remaining options. */
577 while (in < argv + argc)
580 /* Replace the old option list by the new one. */
586 /* Parse all options and non-options as they appear. */
590 prepend_default_options (getenv ("TAR_OPTIONS"), &argc, &argv);
592 while (optchar = getopt_long (argc, argv, OPTION_STRING, long_options, 0),
597 usage (TAREXIT_FAILURE);
603 /* File name or non-parsed option, because of RETURN_IN_ORDER
604 ordering triggered by the leading dash in OPTION_STRING. */
611 set_subcommand_option (CAT_SUBCOMMAND);
614 case OBSOLETE_BLOCK_COMPRESS:
615 WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
618 case OBSOLETE_BLOCKING_FACTOR:
619 WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
625 if (! (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
626 && u == (blocking_factor = u)
627 && 0 < blocking_factor
628 && u == (record_size = u * BLOCKSIZE) / BLOCKSIZE))
629 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
630 _("Invalid blocking factor")));
634 case OBSOLETE_READ_FULL_RECORDS:
636 _("Obsolete option name replaced by --read-full-records")));
640 /* Try to reblock input records. For reading 4.2BSD pipes. */
642 /* It would surely make sense to exchange -B and -R, but it seems
643 that -B has been used for a long while in Sun tar ans most
644 BSD-derived systems. This is a consequence of the block/record
645 terminology confusion. */
647 read_full_records_option = 1;
651 set_subcommand_option (CREATE_SUBCOMMAND);
660 set_subcommand_option (DIFF_SUBCOMMAND);
664 if (archive_names == allocated_archive_names)
666 allocated_archive_names *= 2;
668 xrealloc (archive_name_array,
669 sizeof (const char *) * allocated_archive_names);
671 archive_name_array[archive_names++] = optarg;
675 /* Since -F is only useful with -M, make it implied. Run this
676 script at the end of each tape. */
678 info_script_option = optarg;
679 multi_volume_option = 1;
683 listed_incremental_option = optarg;
684 after_date_option = 1;
688 /* We are making an incremental dump (FIXME: are we?); save
689 directories at the beginning of the archive, and include in each
690 directory its contents. */
692 incremental_option = 1;
696 /* Follow symbolic links. */
698 dereference_option = 1;
702 /* Ignore zero blocks (eofs). This can't be the default,
703 because Unix tar writes two blocks of zeros, then pads out
704 the record with garbage. */
706 ignore_zeros_option = 1;
711 set_use_compress_program_option ("bzip2");
715 /* Don't replace existing files. */
716 old_files_option = KEEP_OLD_FILES;
720 starting_file_option = 1;
725 /* When dumping directories, don't dump files/subdirectories
726 that are on other filesystems. */
728 one_file_system_option = 1;
734 if (xstrtoumax (optarg, 0, 10, &u, "") != LONGINT_OK)
735 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
736 _("Invalid tape length")));
737 tape_length_option = 1024 * (tarlong) u;
738 multi_volume_option = 1;
743 WARN ((0, 0, _("Obsolete option name replaced by --touch")));
751 /* Make multivolume archive: when we can't write any more into
752 the archive, re-open it, and continue writing. */
754 multi_volume_option = 1;
758 recursion_option = 0;
763 after_date_option = 1;
766 case NEWER_MTIME_OPTION:
767 if (newer_mtime_option != TYPE_MINIMUM (time_t))
768 USAGE_ERROR ((0, 0, _("More than one threshold date")));
770 if (FILESYSTEM_PREFIX_LEN (optarg) != 0
775 if (deref_stat (dereference_option, optarg, &st) != 0)
778 USAGE_ERROR ((0, 0, _("Date file not found")));
780 newer_mtime_option = st.st_mtime;
784 newer_mtime_option = get_date (optarg, 0);
785 if (newer_mtime_option == (time_t) -1)
786 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
787 tartime (newer_mtime_option), quote (optarg)));
791 #endif /* not MSDOS */
794 if (archive_format == DEFAULT_FORMAT)
795 archive_format = V7_FORMAT;
796 else if (archive_format != V7_FORMAT)
797 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
801 to_stdout_option = 1;
805 same_permissions_option = 1;
808 case OBSOLETE_ABSOLUTE_NAMES:
809 WARN ((0, 0, _("Obsolete option name replaced by --absolute-names")));
813 absolute_names_option = 1;
817 set_subcommand_option (APPEND_SUBCOMMAND);
820 case OBSOLETE_BLOCK_NUMBER:
821 WARN ((0, 0, _("Obsolete option name replaced by --block-number")));
825 /* Print block numbers for debugging bad tar archives. */
827 /* It would surely make sense to exchange -B and -R, but it seems
828 that -B has been used for a long while in Sun tar ans most
829 BSD-derived systems. This is a consequence of the block/record
830 terminology confusion. */
832 block_number_option = 1;
836 /* Names to extr are sorted. */
838 same_order_option = 1;
846 set_subcommand_option (LIST_SUBCOMMAND);
852 files_from_option = optarg;
856 set_subcommand_option (UPDATE_SUBCOMMAND);
860 old_files_option = UNLINK_FIRST_OLD_FILES;
868 volume_label_option = optarg;
872 interactive_option = 1;
880 set_subcommand_option (EXTRACT_SUBCOMMAND);
884 if (add_exclude_file (add_exclude, excluded, optarg,
885 exclude_options | recursion_option, '\n')
889 FATAL_ERROR ((0, e, "%s", quotearg_colon (optarg)));
894 set_use_compress_program_option ("gzip");
898 set_use_compress_program_option ("compress");
901 case OBSOLETE_VERSION_CONTROL:
902 WARN ((0, 0, _("Obsolete option name replaced by --backup")));
905 case ANCHORED_OPTION:
906 exclude_options |= EXCLUDE_ANCHORED;
912 version_control_string = optarg;
916 set_subcommand_option (DELETE_SUBCOMMAND);
920 add_exclude (excluded, optarg, exclude_options | recursion_option);
923 case IGNORE_CASE_OPTION:
924 exclude_options |= FNM_CASEFOLD;
928 if (! (strlen (optarg) < GNAME_FIELD_SIZE
929 && gname_to_gid (optarg, &group_option)))
932 if (xstrtoumax (optarg, 0, 10, &g, "") == LONGINT_OK
936 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
937 _("%s: Invalid group")));
943 = mode_compile (optarg,
944 MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
945 if (mode_option == MODE_INVALID)
946 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
947 if (mode_option == MODE_MEMORY_EXHAUSTED)
951 case NO_ANCHORED_OPTION:
952 exclude_options &= ~ EXCLUDE_ANCHORED;
955 case NO_IGNORE_CASE_OPTION:
956 exclude_options &= ~ FNM_CASEFOLD;
959 case NO_WILDCARDS_OPTION:
960 exclude_options &= ~ EXCLUDE_WILDCARDS;
963 case NO_WILDCARDS_MATCH_SLASH_OPTION:
964 exclude_options |= FNM_FILE_NAME;
968 filename_terminator = '\0';
971 case OVERWRITE_OPTION:
972 old_files_option = OVERWRITE_OLD_FILES;
975 case OVERWRITE_DIR_OPTION:
976 old_files_option = OVERWRITE_OLD_DIRS;
980 if (! (strlen (optarg) < UNAME_FIELD_SIZE
981 && uname_to_uid (optarg, &owner_option)))
984 if (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
988 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
989 _("Invalid owner")));
994 #if OLDGNU_COMPATIBILITY
995 if (archive_format == DEFAULT_FORMAT)
996 archive_format = GNU_FORMAT;
997 else if (archive_format != GNU_FORMAT)
998 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
1000 if (archive_format == DEFAULT_FORMAT)
1001 archive_format = POSIX_FORMAT;
1002 else if (archive_format != POSIX_FORMAT)
1003 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
1007 case PRESERVE_OPTION:
1008 same_permissions_option = 1;
1009 same_order_option = 1;
1012 case RECORD_SIZE_OPTION:
1015 if (! (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
1016 && u == (size_t) u))
1017 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
1018 _("Invalid record size")));
1020 if (record_size % BLOCKSIZE != 0)
1021 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
1023 blocking_factor = record_size / BLOCKSIZE;
1027 case RSH_COMMAND_OPTION:
1028 rsh_command_option = optarg;
1033 backup_suffix_string = optarg;
1036 case USE_COMPRESS_PROGRAM_OPTION:
1037 set_use_compress_program_option (optarg);
1040 case VOLNO_FILE_OPTION:
1041 volno_file_option = optarg;
1044 case WILDCARDS_OPTION:
1045 exclude_options |= EXCLUDE_WILDCARDS;
1048 case WILDCARDS_MATCH_SLASH_OPTION:
1049 exclude_options &= ~ FNM_FILE_NAME;
1061 #ifdef DEVICE_PREFIX
1063 int device = optchar - '0';
1065 static char buf[sizeof DEVICE_PREFIX + 10];
1068 density = getopt_long (argc, argv, "lmh", 0, 0);
1069 strcpy (buf, DEVICE_PREFIX);
1070 cursor = buf + strlen (buf);
1072 #ifdef DENSITY_LETTER
1074 sprintf (cursor, "%d%c", device, density);
1076 #else /* not DENSITY_LETTER */
1103 usage (TAREXIT_FAILURE);
1105 sprintf (cursor, "%d", device);
1107 #endif /* not DENSITY_LETTER */
1109 if (archive_names == allocated_archive_names)
1111 allocated_archive_names *= 2;
1112 archive_name_array =
1113 xrealloc (archive_name_array,
1114 sizeof (const char *) * allocated_archive_names);
1116 archive_name_array[archive_names++] = buf;
1118 /* FIXME: How comes this works for many archives when buf is
1123 #else /* not DEVICE_PREFIX */
1126 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1128 #endif /* not DEVICE_PREFIX */
1131 /* Handle operands after any "--" argument. */
1132 for (; optind < argc; optind++)
1134 name_add (argv[optind]);
1138 /* Process trivial options. */
1142 printf ("tar (GNU %s) %s\n", PACKAGE, VERSION);
1143 print_copyright ("2001 Free Software Foundation, Inc.");
1145 This program comes with NO WARRANTY, to the extent permitted by law.\n\
1146 You may redistribute it under the terms of the GNU General Public License;\n\
1147 see the file named COPYING for details."));
1149 puts (_("Written by John Gilmore and Jay Fenlason."));
1151 exit (TAREXIT_SUCCESS);
1155 usage (TAREXIT_SUCCESS);
1157 /* Derive option values and check option consistency. */
1159 if (archive_format == DEFAULT_FORMAT)
1161 #if OLDGNU_COMPATIBILITY
1162 archive_format = OLDGNU_FORMAT;
1164 archive_format = GNU_FORMAT;
1168 if (archive_format == GNU_FORMAT && getenv ("POSIXLY_CORRECT"))
1169 archive_format = POSIX_FORMAT;
1171 if ((volume_label_option
1172 || incremental_option || multi_volume_option || sparse_option)
1173 && archive_format != OLDGNU_FORMAT && archive_format != GNU_FORMAT)
1175 _("GNU features wanted on incompatible archive format")));
1177 if (archive_names == 0)
1179 /* If no archive file name given, try TAPE from the environment, or
1180 else, DEFAULT_ARCHIVE from the configuration process. */
1183 archive_name_array[0] = getenv ("TAPE");
1184 if (! archive_name_array[0])
1185 archive_name_array[0] = DEFAULT_ARCHIVE;
1188 /* Allow multiple archives only with `-M'. */
1190 if (archive_names > 1 && !multi_volume_option)
1192 _("Multiple archive files requires `-M' option")));
1194 if (listed_incremental_option
1195 && newer_mtime_option != TYPE_MINIMUM (time_t))
1197 _("Cannot combine --listed-incremental with --newer")));
1199 if (volume_label_option)
1201 size_t volume_label_max_len =
1202 (sizeof current_header->header.name
1203 - 1 /* for trailing '\0' */
1204 - (multi_volume_option
1205 ? (sizeof " Volume "
1206 - 1 /* for null at end of " Volume " */
1207 + INT_STRLEN_BOUND (int) /* for volume number */
1208 - 1 /* for sign, as 0 <= volno */)
1210 if (volume_label_max_len < strlen (volume_label_option))
1212 _("%s: Volume label is too long (limit is %lu bytes)"),
1213 quotearg_colon (volume_label_option),
1214 (unsigned long) volume_label_max_len));
1217 /* If ready to unlink hierarchies, so we are for simpler files. */
1218 if (recursive_unlink_option)
1219 old_files_option = UNLINK_FIRST_OLD_FILES;
1221 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1222 explicit or implied, is used correctly. */
1224 switch (subcommand_option)
1226 case CREATE_SUBCOMMAND:
1227 if (input_files == 0 && !files_from_option)
1229 _("Cowardly refusing to create an empty archive")));
1232 case EXTRACT_SUBCOMMAND:
1233 case LIST_SUBCOMMAND:
1234 case DIFF_SUBCOMMAND:
1235 for (archive_name_cursor = archive_name_array;
1236 archive_name_cursor < archive_name_array + archive_names;
1237 archive_name_cursor++)
1238 if (!strcmp (*archive_name_cursor, "-"))
1239 request_stdin ("-f");
1242 case CAT_SUBCOMMAND:
1243 case UPDATE_SUBCOMMAND:
1244 case APPEND_SUBCOMMAND:
1245 for (archive_name_cursor = archive_name_array;
1246 archive_name_cursor < archive_name_array + archive_names;
1247 archive_name_cursor++)
1248 if (!strcmp (*archive_name_cursor, "-"))
1250 _("Options `-Aru' are incompatible with `-f -'")));
1256 archive_name_cursor = archive_name_array;
1258 /* Prepare for generating backup names. */
1260 if (backup_suffix_string)
1261 simple_backup_suffix = xstrdup (backup_suffix_string);
1264 backup_type = xget_version ("--backup", version_control_string);
1269 /* Main routine for tar. */
1271 main (int argc, char **argv)
1273 #if HAVE_CLOCK_GETTIME
1274 if (clock_gettime (CLOCK_REALTIME, &start_timespec) != 0)
1276 start_time = time (0);
1277 program_name = argv[0];
1278 (void) setlocale (LC_ALL, "");
1279 bindtextdomain (PACKAGE, LOCALEDIR);
1280 textdomain (PACKAGE);
1282 exit_status = TAREXIT_SUCCESS;
1283 filename_terminator = '\n';
1284 set_quoting_style (0, escape_quoting_style);
1286 /* Pre-allocate a few structures. */
1288 allocated_archive_names = 10;
1289 archive_name_array =
1290 xmalloc (sizeof (const char *) * allocated_archive_names);
1294 /* System V fork+wait does not work if SIGCHLD is ignored. */
1295 signal (SIGCHLD, SIG_DFL);
1300 /* Decode options. */
1302 decode_options (argc, argv);
1303 name_init (argc, argv);
1305 /* Main command execution. */
1307 if (volno_file_option)
1308 init_volume_number ();
1310 switch (subcommand_option)
1312 case UNKNOWN_SUBCOMMAND:
1314 _("You must specify one of the `-Acdtrux' options")));
1316 case CAT_SUBCOMMAND:
1317 case UPDATE_SUBCOMMAND:
1318 case APPEND_SUBCOMMAND:
1322 case DELETE_SUBCOMMAND:
1323 delete_archive_members ();
1326 case CREATE_SUBCOMMAND:
1331 print_total_written ();
1334 case EXTRACT_SUBCOMMAND:
1336 read_and (extract_archive);
1340 case LIST_SUBCOMMAND:
1341 read_and (list_archive);
1344 case DIFF_SUBCOMMAND:
1346 read_and (diff_archive);
1350 if (volno_file_option)
1351 closeout_volume_number ();
1353 /* Dispose of allocated memory, and return. */
1355 free (archive_name_array);
1358 if (stdlis == stdout && (ferror (stdout) || fclose (stdout) != 0))
1359 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1360 if (exit_status == TAREXIT_FAILURE)
1361 error (0, 0, _("Error exit delayed from previous errors"));