1 /* A tar (tape archiver) program.
3 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
4 2001, 2003 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. */
28 #if ! defined SIGCHLD && defined SIGCLD
29 # define SIGCHLD SIGCLD
32 /* The following causes "common.h" to produce definitions of all the global
33 variables, rather than just "extern" declarations of them. GNU tar does
34 depend on the system loader to preset all GLOBAL variables to neutral (or
35 zero) values; explicit initialization is usually not done. */
40 #include <localedir.h>
45 /* Local declarations. */
47 #ifndef DEFAULT_ARCHIVE
48 # define DEFAULT_ARCHIVE "tar.out"
51 #ifndef DEFAULT_BLOCKING
52 # define DEFAULT_BLOCKING 20
55 static void usage (int) __attribute__ ((noreturn
));
59 /* Name of option using stdin. */
60 static const char *stdin_used_by
;
62 /* Doesn't return if stdin already requested. */
64 request_stdin (const char *option
)
67 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
68 stdin_used_by
, option
));
70 stdin_used_by
= option
;
73 /* Returns true if and only if the user typed 'y' or 'Y'. */
75 confirm (const char *message_action
, const char *message_name
)
77 static FILE *confirm_file
;
78 static int confirm_file_EOF
;
82 if (archive
== 0 || stdin_used_by
)
84 confirm_file
= fopen (TTY_NAME
, "r");
86 open_fatal (TTY_NAME
);
95 fprintf (stdlis
, "%s %s?", message_action
, quote (message_name
));
99 int reply
= confirm_file_EOF
? EOF
: getc (confirm_file
);
102 for (character
= reply
;
104 character
= getc (confirm_file
))
105 if (character
== EOF
)
107 confirm_file_EOF
= 1;
108 fputc ('\n', stdlis
);
112 return reply
== 'y' || reply
== 'Y';
118 /* For long options that unconditionally set a single flag, we have getopt
119 do it. For the others, we share the code for the equivalent short
120 named option, the name of which is stored in the otherwise-unused `val'
121 field of the `struct option'; for long options that have no equivalent
122 short option, we use non-characters as pseudo short options,
123 starting at CHAR_MAX + 1 and going upwards. */
127 ANCHORED_OPTION
= CHAR_MAX
+ 1,
128 ATIME_PRESERVE_OPTION
,
136 IGNORE_FAILED_READ_OPTION
,
141 NO_IGNORE_CASE_OPTION
,
142 NO_OVERWRITE_DIR_OPTION
,
144 NO_WILDCARDS_MATCH_SLASH_OPTION
,
146 NUMERIC_OWNER_OPTION
,
152 RECURSIVE_UNLINK_OPTION
,
155 SHOW_OMITTED_DIRS_OPTION
,
158 USE_COMPRESS_PROGRAM_OPTION
,
161 WILDCARDS_MATCH_SLASH_OPTION
,
163 /* Some cleanup is being made in GNU tar long options. Using old names is
164 allowed for a while, but will also send a warning to stderr. Take old
165 names out in 1.14, or in summer 1997, whichever happens last. */
167 OBSOLETE_ABSOLUTE_NAMES
,
168 OBSOLETE_BLOCK_COMPRESS
,
169 OBSOLETE_BLOCKING_FACTOR
,
170 OBSOLETE_BLOCK_NUMBER
,
171 OBSOLETE_READ_FULL_RECORDS
,
173 OBSOLETE_VERSION_CONTROL
176 /* If nonzero, display usage information and exit. */
177 static int show_help
;
179 /* If nonzero, print the version on standard output and exit. */
180 static int show_version
;
182 static struct option long_options
[] =
184 {"absolute-names", no_argument
, 0, 'P'},
185 {"absolute-paths", no_argument
, 0, OBSOLETE_ABSOLUTE_NAMES
},
186 {"after-date", required_argument
, 0, 'N'},
187 {"anchored", no_argument
, 0, ANCHORED_OPTION
},
188 {"append", no_argument
, 0, 'r'},
189 {"atime-preserve", no_argument
, 0, ATIME_PRESERVE_OPTION
},
190 {"backup", optional_argument
, 0, BACKUP_OPTION
},
191 {"block-compress", no_argument
, 0, OBSOLETE_BLOCK_COMPRESS
},
192 {"block-number", no_argument
, 0, 'R'},
193 {"block-size", required_argument
, 0, OBSOLETE_BLOCKING_FACTOR
},
194 {"blocking-factor", required_argument
, 0, 'b'},
195 {"bzip2", no_argument
, 0, 'j'},
196 {"catenate", no_argument
, 0, 'A'},
197 {"checkpoint", no_argument
, 0, CHECKPOINT_OPTION
},
198 {"compare", no_argument
, 0, 'd'},
199 {"compress", no_argument
, 0, 'Z'},
200 {"concatenate", no_argument
, 0, 'A'},
201 {"confirmation", no_argument
, 0, 'w'},
202 /* FIXME: --selective as a synonym for --confirmation? */
203 {"create", no_argument
, 0, 'c'},
204 {"delete", no_argument
, 0, DELETE_OPTION
},
205 {"dereference", no_argument
, 0, 'h'},
206 {"diff", no_argument
, 0, 'd'},
207 {"directory", required_argument
, 0, 'C'},
208 {"exclude", required_argument
, 0, EXCLUDE_OPTION
},
209 {"exclude-from", required_argument
, 0, 'X'},
210 {"extract", no_argument
, 0, 'x'},
211 {"file", required_argument
, 0, 'f'},
212 {"files-from", required_argument
, 0, 'T'},
213 {"force-local", no_argument
, 0, FORCE_LOCAL_OPTION
},
214 {"get", no_argument
, 0, 'x'},
215 {"group", required_argument
, 0, GROUP_OPTION
},
216 {"gunzip", no_argument
, 0, 'z'},
217 {"gzip", no_argument
, 0, 'z'},
218 {"help", no_argument
, &show_help
, 1},
219 {"ignore-case", no_argument
, 0, IGNORE_CASE_OPTION
},
220 {"ignore-failed-read", no_argument
, 0, IGNORE_FAILED_READ_OPTION
},
221 {"ignore-zeros", no_argument
, 0, 'i'},
222 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
223 {"incremental", no_argument
, 0, 'G'},
224 {"index-file", required_argument
, 0, INDEX_FILE_OPTION
},
225 {"info-script", required_argument
, 0, 'F'},
226 {"interactive", no_argument
, 0, 'w'},
227 {"keep-old-files", no_argument
, 0, 'k'},
228 {"label", required_argument
, 0, 'V'},
229 {"list", no_argument
, 0, 't'},
230 {"listed-incremental", required_argument
, 0, 'g'},
231 {"mode", required_argument
, 0, MODE_OPTION
},
232 {"modification-time", no_argument
, 0, OBSOLETE_TOUCH
},
233 {"multi-volume", no_argument
, 0, 'M'},
234 {"new-volume-script", required_argument
, 0, 'F'},
235 {"newer", required_argument
, 0, 'N'},
236 {"newer-mtime", required_argument
, 0, NEWER_MTIME_OPTION
},
237 {"null", no_argument
, 0, NULL_OPTION
},
238 {"no-anchored", no_argument
, 0, NO_ANCHORED_OPTION
},
239 {"no-ignore-case", no_argument
, 0, NO_IGNORE_CASE_OPTION
},
240 {"no-overwrite-dir", no_argument
, 0, NO_OVERWRITE_DIR_OPTION
},
241 {"no-wildcards", no_argument
, 0, NO_WILDCARDS_OPTION
},
242 {"no-wildcards-match-slash", no_argument
, 0, NO_WILDCARDS_MATCH_SLASH_OPTION
},
243 {"no-recursion", no_argument
, &recursion_option
, 0},
244 {"no-same-owner", no_argument
, &same_owner_option
, -1},
245 {"no-same-permissions", no_argument
, &same_permissions_option
, -1},
246 {"numeric-owner", no_argument
, 0, NUMERIC_OWNER_OPTION
},
247 {"old-archive", no_argument
, 0, 'o'},
248 {"one-file-system", no_argument
, 0, 'l'},
249 {"overwrite", no_argument
, 0, OVERWRITE_OPTION
},
250 {"owner", required_argument
, 0, OWNER_OPTION
},
251 {"portability", no_argument
, 0, 'o'},
252 {"posix", no_argument
, 0, POSIX_OPTION
},
253 {"preserve", no_argument
, 0, PRESERVE_OPTION
},
254 {"preserve-order", no_argument
, 0, 's'},
255 {"preserve-permissions", no_argument
, 0, 'p'},
256 {"recursion", no_argument
, &recursion_option
, FNM_LEADING_DIR
},
257 {"recursive-unlink", no_argument
, 0, RECURSIVE_UNLINK_OPTION
},
258 {"read-full-blocks", no_argument
, 0, OBSOLETE_READ_FULL_RECORDS
},
259 {"read-full-records", no_argument
, 0, 'B'},
260 /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
261 {"record-number", no_argument
, 0, OBSOLETE_BLOCK_NUMBER
},
262 {"record-size", required_argument
, 0, RECORD_SIZE_OPTION
},
263 {"remove-files", no_argument
, 0, REMOVE_FILES_OPTION
},
264 {"rsh-command", required_argument
, 0, RSH_COMMAND_OPTION
},
265 {"same-order", no_argument
, 0, 's'},
266 {"same-owner", no_argument
, &same_owner_option
, 1},
267 {"same-permissions", no_argument
, 0, 'p'},
268 {"show-omitted-dirs", no_argument
, 0, SHOW_OMITTED_DIRS_OPTION
},
269 {"sparse", no_argument
, 0, 'S'},
270 {"starting-file", required_argument
, 0, 'K'},
271 {"suffix", required_argument
, 0, SUFFIX_OPTION
},
272 {"tape-length", required_argument
, 0, 'L'},
273 {"to-stdout", no_argument
, 0, 'O'},
274 {"totals", no_argument
, 0, TOTALS_OPTION
},
275 {"touch", no_argument
, 0, 'm'},
276 {"uncompress", no_argument
, 0, 'Z'},
277 {"ungzip", no_argument
, 0, 'z'},
278 {"unlink-first", no_argument
, 0, 'U'},
279 {"update", no_argument
, 0, 'u'},
280 {"use-compress-program", required_argument
, 0, USE_COMPRESS_PROGRAM_OPTION
},
281 {"verbose", no_argument
, 0, 'v'},
282 {"verify", no_argument
, 0, 'W'},
283 {"version", no_argument
, &show_version
, 1},
284 {"version-control", required_argument
, 0, OBSOLETE_VERSION_CONTROL
},
285 {"volno-file", required_argument
, 0, VOLNO_FILE_OPTION
},
286 {"wildcards", no_argument
, 0, WILDCARDS_OPTION
},
287 {"wildcards-match-slash", no_argument
, 0, WILDCARDS_MATCH_SLASH_OPTION
},
292 /* Print a usage message and exit with STATUS. */
296 if (status
!= TAREXIT_SUCCESS
)
297 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
302 GNU `tar' saves many files together into a single tape or disk archive, and\n\
303 can restore individual files from the archive.\n"),
305 printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
308 %s -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
309 %s -tvf archive.tar # List all files in archive.tar verbosely.\n\
310 %s -xf archive.tar # Extract all files from archive.tar.\n"),
311 program_name
, program_name
, program_name
, program_name
);
314 If a long option shows an argument as mandatory, then it is mandatory\n\
315 for the equivalent short option also. Similarly for optional arguments.\n"),
319 Main operation mode:\n\
320 -t, --list list the contents of an archive\n\
321 -x, --extract, --get extract files from an archive\n\
322 -c, --create create a new archive\n\
323 -d, --diff, --compare find differences between archive and file system\n\
324 -r, --append append files to the end of an archive\n\
325 -u, --update only append files newer than copy in archive\n\
326 -A, --catenate append tar files to an archive\n\
327 --concatenate same as -A\n\
328 --delete delete from the archive (not on mag tapes!)\n"),
332 Operation modifiers:\n\
333 -W, --verify attempt to verify the archive after writing it\n\
334 --remove-files remove files after adding them to the archive\n\
335 -k, --keep-old-files don't replace existing files when extracting\n\
336 --overwrite overwrite existing files when extracting\n\
337 --no-overwrite-dir preserve metadata of existing directories\n\
338 -U, --unlink-first remove each file prior to extracting over it\n\
339 --recursive-unlink empty hierarchies prior to extracting directory\n\
340 -S, --sparse handle sparse files efficiently\n\
341 -O, --to-stdout extract files to standard output\n\
342 -G, --incremental handle old GNU-format incremental backup\n\
343 -g, --listed-incremental=FILE\n\
344 handle new GNU-format incremental backup\n\
345 --ignore-failed-read do not exit with nonzero on unreadable files\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 --no-same-owner extract files as yourself\n\
357 --numeric-owner always use numbers for user/group names\n\
358 -p, --same-permissions extract permissions information\n\
359 --no-same-permissions do not extract permissions information\n\
360 --preserve-permissions same as -p\n\
361 -s, --same-order sort names to extract to match archive\n\
362 --preserve-order same as -s\n\
363 --preserve same as both -p and -s\n"),
367 Device selection and switching:\n\
368 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
369 --force-local archive file is local even if has a colon\n\
370 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
371 -[0-7][lmh] specify drive and density\n\
372 -M, --multi-volume create/list/extract multi-volume archive\n\
373 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
374 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
375 --new-volume-script=FILE same as -F FILE\n\
376 --volno-file=FILE use/update the volume number in FILE\n"),
381 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
382 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
383 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
384 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
388 Archive format selection:\n\
389 -V, --label=NAME create archive with volume name NAME\n\
390 PATTERN at list/extract time, a globbing PATTERN\n\
391 -o, --old-archive, --portability write a V7 format archive\n\
392 --posix write a POSIX format archive\n\
393 -j, --bzip2 filter the archive through bzip2\n\
394 -z, --gzip, --ungzip filter the archive through gzip\n\
395 -Z, --compress, --uncompress filter the archive through compress\n\
396 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
400 Local file selection:\n\
401 -C, --directory=DIR change to directory DIR\n\
402 -T, --files-from=NAME get names to extract or create from file NAME\n\
403 --null -T reads null-terminated names, disable -C\n\
404 --exclude=PATTERN exclude files, given as a PATTERN\n\
405 -X, --exclude-from=FILE exclude patterns listed in FILE\n\
406 --anchored exclude patterns match file name start (default)\n\
407 --no-anchored exclude patterns match after any /\n\
408 --ignore-case exclusion ignores case\n\
409 --no-ignore-case exclusion is case sensitive (default)\n\
410 --wildcards exclude patterns use wildcards (default)\n\
411 --no-wildcards exclude patterns are plain strings\n\
412 --wildcards-match-slash exclude pattern wildcards match '/' (default)\n\
413 --no-wildcards-match-slash exclude pattern wildcards do not match '/'\n\
414 -P, --absolute-names don't strip leading `/'s from file names\n\
415 -h, --dereference dump instead the files symlinks point to\n\
416 --no-recursion avoid descending automatically in directories\n\
417 -l, --one-file-system stay in local file system when creating archive\n\
418 -K, --starting-file=NAME begin at file NAME in the archive\n"),
422 -N, --newer=DATE only store files newer than DATE\n\
423 --newer-mtime=DATE compare date and time when data changed only\n\
424 --after-date=DATE same as -N\n"),
428 --backup[=CONTROL] backup before removal, choose version control\n\
429 --suffix=SUFFIX backup before removal, override usual suffix\n"),
433 Informative output:\n\
434 --help print this help, then exit\n\
435 --version print tar program version number, then exit\n\
436 -v, --verbose verbosely list files processed\n\
437 --checkpoint print directory names while reading the archive\n\
438 --totals print total bytes written while creating archive\n\
439 --index-file=FILE send verbose output to FILE\n\
440 -R, --block-number show block number within archive with each message\n\
441 -w, --interactive ask for confirmation for every action\n\
442 --confirmation same as -w\n"),
446 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
447 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
449 t, numbered make numbered backups\n\
450 nil, existing numbered if numbered backups exist, simple otherwise\n\
451 never, simple always make simple backups\n"),
455 GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
456 is set in the environment, GNU extensions are disallowed with `--posix'.\n\
457 Support for POSIX is only partially implemented, don't count on it yet.\n\
458 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; DATE may be a textual date\n\
459 or a file name starting with `/' or `.', in which case the file's date is used.\n\
460 *This* `tar' defaults to `-f%s -b%d'.\n"),
461 DEFAULT_ARCHIVE
, DEFAULT_BLOCKING
);
462 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
467 /* Parse the options for tar. */
469 /* Available option letters are DEHIJQY and aenqy. Some are reserved:
471 e exit immediately with a nonzero exit status if unexpected errors occur
472 E use extended headers (draft POSIX headers, that is)
473 I same as T (for compatibility with Solaris tar)
474 n the archive is quickly seekable, so don't worry about random seeks
475 q stop after extracting the first occurrence of the named file
476 y per-file gzip compression
477 Y per-block gzip compression */
479 #define OPTION_STRING \
480 "-01234567ABC:F:GIK:L:MN:OPRST:UV:WX:Zb:cdf:g:hijklmoprstuvwxyz"
483 set_subcommand_option (enum subcommand subcommand
)
485 if (subcommand_option
!= UNKNOWN_SUBCOMMAND
486 && subcommand_option
!= subcommand
)
488 _("You may not specify more than one `-Acdtrux' option")));
490 subcommand_option
= subcommand
;
494 set_use_compress_program_option (const char *string
)
496 if (use_compress_program_option
&& strcmp (use_compress_program_option
, string
) != 0)
497 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
499 use_compress_program_option
= string
;
503 decode_options (int argc
, char **argv
)
505 int optchar
; /* option letter */
506 int input_files
; /* number of input files */
507 char const *textual_date_option
= 0;
508 char const *backup_suffix_string
;
509 char const *version_control_string
= 0;
510 int exclude_options
= EXCLUDE_WILDCARDS
;
512 /* Set some default option values. */
514 subcommand_option
= UNKNOWN_SUBCOMMAND
;
515 archive_format
= DEFAULT_FORMAT
;
516 blocking_factor
= DEFAULT_BLOCKING
;
517 record_size
= DEFAULT_BLOCKING
* BLOCKSIZE
;
518 excluded
= new_exclude ();
519 newer_mtime_option
= TYPE_MINIMUM (time_t);
520 recursion_option
= FNM_LEADING_DIR
;
525 backup_suffix_string
= getenv ("SIMPLE_BACKUP_SUFFIX");
527 /* Convert old-style tar call by exploding option element and rearranging
528 options accordingly. */
530 if (argc
> 1 && argv
[1][0] != '-')
532 int new_argc
; /* argc value for rearranged arguments */
533 char **new_argv
; /* argv value for rearranged arguments */
534 char *const *in
; /* cursor into original argv */
535 char **out
; /* cursor into rearranged argv */
536 const char *letter
; /* cursor into old option letters */
537 char buffer
[3]; /* constructed option buffer */
538 const char *cursor
; /* cursor in OPTION_STRING */
540 /* Initialize a constructed option. */
545 /* Allocate a new argument array, and copy program name in it. */
547 new_argc
= argc
- 1 + strlen (argv
[1]);
548 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (char *));
553 /* Copy each old letter option as a separate option, and have the
554 corresponding argument moved next to it. */
556 for (letter
= *in
++; *letter
; letter
++)
559 *out
++ = xstrdup (buffer
);
560 cursor
= strchr (OPTION_STRING
, *letter
);
561 if (cursor
&& cursor
[1] == ':')
563 if (in
< argv
+ argc
)
566 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
571 /* Copy all remaining options. */
573 while (in
< argv
+ argc
)
577 /* Replace the old option list by the new one. */
583 /* Parse all options and non-options as they appear. */
587 prepend_default_options (getenv ("TAR_OPTIONS"), &argc
, &argv
);
589 while (optchar
= getopt_long (argc
, argv
, OPTION_STRING
, long_options
, 0),
594 usage (TAREXIT_FAILURE
);
600 /* File name or non-parsed option, because of RETURN_IN_ORDER
601 ordering triggered by the leading dash in OPTION_STRING. */
608 set_subcommand_option (CAT_SUBCOMMAND
);
611 case OBSOLETE_BLOCK_COMPRESS
:
612 WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
615 case OBSOLETE_BLOCKING_FACTOR
:
616 WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
622 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
623 && u
== (blocking_factor
= u
)
624 && 0 < blocking_factor
625 && u
== (record_size
= u
* BLOCKSIZE
) / BLOCKSIZE
))
626 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
627 _("Invalid blocking factor")));
631 case OBSOLETE_READ_FULL_RECORDS
:
633 _("Obsolete option name replaced by --read-full-records")));
637 /* Try to reblock input records. For reading 4.2BSD pipes. */
639 /* It would surely make sense to exchange -B and -R, but it seems
640 that -B has been used for a long while in Sun tar ans most
641 BSD-derived systems. This is a consequence of the block/record
642 terminology confusion. */
644 read_full_records_option
= 1;
648 set_subcommand_option (CREATE_SUBCOMMAND
);
657 set_subcommand_option (DIFF_SUBCOMMAND
);
661 if (archive_names
== allocated_archive_names
)
663 allocated_archive_names
*= 2;
665 xrealloc (archive_name_array
,
666 sizeof (const char *) * allocated_archive_names
);
668 archive_name_array
[archive_names
++] = optarg
;
672 /* Since -F is only useful with -M, make it implied. Run this
673 script at the end of each tape. */
675 info_script_option
= optarg
;
676 multi_volume_option
= 1;
680 listed_incremental_option
= optarg
;
681 after_date_option
= 1;
685 /* We are making an incremental dump (FIXME: are we?); save
686 directories at the beginning of the archive, and include in each
687 directory its contents. */
689 incremental_option
= 1;
693 /* Follow symbolic links. */
695 dereference_option
= 1;
699 /* Ignore zero blocks (eofs). This can't be the default,
700 because Unix tar writes two blocks of zeros, then pads out
701 the record with garbage. */
703 ignore_zeros_option
= 1;
708 _("Warning: the -I option is not supported;"
709 " perhaps you meant -j or -T?")));
713 set_use_compress_program_option ("bzip2");
717 /* Don't replace existing files. */
718 old_files_option
= KEEP_OLD_FILES
;
722 starting_file_option
= 1;
727 /* When dumping directories, don't dump files/subdirectories
728 that are on other filesystems. */
730 one_file_system_option
= 1;
736 if (xstrtoumax (optarg
, 0, 10, &u
, "") != LONGINT_OK
)
737 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
738 _("Invalid tape length")));
739 tape_length_option
= 1024 * (tarlong
) u
;
740 multi_volume_option
= 1;
745 WARN ((0, 0, _("Obsolete option name replaced by --touch")));
753 /* Make multivolume archive: when we can't write any more into
754 the archive, re-open it, and continue writing. */
756 multi_volume_option
= 1;
761 after_date_option
= 1;
764 case NEWER_MTIME_OPTION
:
765 if (newer_mtime_option
!= TYPE_MINIMUM (time_t))
766 USAGE_ERROR ((0, 0, _("More than one threshold date")));
768 if (FILESYSTEM_PREFIX_LEN (optarg
) != 0
773 if (deref_stat (dereference_option
, optarg
, &st
) != 0)
776 USAGE_ERROR ((0, 0, _("Date file not found")));
778 newer_mtime_option
= st
.st_mtime
;
782 newer_mtime_option
= get_date (optarg
, 0);
783 if (newer_mtime_option
== (time_t) -1)
784 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
785 tartime (newer_mtime_option
), quote (optarg
)));
787 textual_date_option
= 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
);
851 files_from_option
= optarg
;
855 set_subcommand_option (UPDATE_SUBCOMMAND
);
859 old_files_option
= UNLINK_FIRST_OLD_FILES
;
867 volume_label_option
= optarg
;
871 interactive_option
= 1;
879 set_subcommand_option (EXTRACT_SUBCOMMAND
);
883 if (add_exclude_file (add_exclude
, excluded
, optarg
,
884 exclude_options
| recursion_option
, '\n')
888 FATAL_ERROR ((0, e
, "%s", quotearg_colon (optarg
)));
894 _("Warning: the -y option is not supported;"
895 " perhaps you meant -j?")));
899 set_use_compress_program_option ("gzip");
903 set_use_compress_program_option ("compress");
906 case OBSOLETE_VERSION_CONTROL
:
907 WARN ((0, 0, _("Obsolete option name replaced by --backup")));
910 case ANCHORED_OPTION
:
911 exclude_options
|= EXCLUDE_ANCHORED
;
914 case ATIME_PRESERVE_OPTION
:
915 atime_preserve_option
= 1;
918 case CHECKPOINT_OPTION
:
919 checkpoint_option
= 1;
925 version_control_string
= optarg
;
929 set_subcommand_option (DELETE_SUBCOMMAND
);
933 add_exclude (excluded
, optarg
, exclude_options
| recursion_option
);
936 case FORCE_LOCAL_OPTION
:
937 force_local_option
= 1;
940 case INDEX_FILE_OPTION
:
941 index_file_name
= optarg
;
944 case IGNORE_CASE_OPTION
:
945 exclude_options
|= FNM_CASEFOLD
;
948 case IGNORE_FAILED_READ_OPTION
:
949 ignore_failed_read_option
= 1;
953 if (! (strlen (optarg
) < GNAME_FIELD_SIZE
954 && gname_to_gid (optarg
, &group_option
)))
957 if (xstrtoumax (optarg
, 0, 10, &g
, "") == LONGINT_OK
961 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
962 _("%s: Invalid group")));
968 = mode_compile (optarg
,
969 MODE_MASK_EQUALS
| MODE_MASK_PLUS
| MODE_MASK_MINUS
);
970 if (mode_option
== MODE_INVALID
)
971 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
972 if (mode_option
== MODE_MEMORY_EXHAUSTED
)
976 case NO_ANCHORED_OPTION
:
977 exclude_options
&= ~ EXCLUDE_ANCHORED
;
980 case NO_IGNORE_CASE_OPTION
:
981 exclude_options
&= ~ FNM_CASEFOLD
;
984 case NO_OVERWRITE_DIR_OPTION
:
985 old_files_option
= NO_OVERWRITE_DIR_OLD_FILES
;
988 case NO_WILDCARDS_OPTION
:
989 exclude_options
&= ~ EXCLUDE_WILDCARDS
;
992 case NO_WILDCARDS_MATCH_SLASH_OPTION
:
993 exclude_options
|= FNM_FILE_NAME
;
997 filename_terminator
= '\0';
1000 case NUMERIC_OWNER_OPTION
:
1001 numeric_owner_option
= 1;
1004 case OVERWRITE_OPTION
:
1005 old_files_option
= OVERWRITE_OLD_FILES
;
1009 if (! (strlen (optarg
) < UNAME_FIELD_SIZE
1010 && uname_to_uid (optarg
, &owner_option
)))
1013 if (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
1017 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1018 _("Invalid owner")));
1023 #if OLDGNU_COMPATIBILITY
1024 if (archive_format
== DEFAULT_FORMAT
)
1025 archive_format
= GNU_FORMAT
;
1026 else if (archive_format
!= GNU_FORMAT
)
1027 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
1029 if (archive_format
== DEFAULT_FORMAT
)
1030 archive_format
= POSIX_FORMAT
;
1031 else if (archive_format
!= POSIX_FORMAT
)
1032 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
1036 case PRESERVE_OPTION
:
1037 same_permissions_option
= 1;
1038 same_order_option
= 1;
1041 case RECORD_SIZE_OPTION
:
1044 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
1045 && u
== (size_t) u
))
1046 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1047 _("Invalid record size")));
1049 if (record_size
% BLOCKSIZE
!= 0)
1050 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
1052 blocking_factor
= record_size
/ BLOCKSIZE
;
1056 case RECURSIVE_UNLINK_OPTION
:
1057 recursive_unlink_option
= 1;
1060 case REMOVE_FILES_OPTION
:
1061 remove_files_option
= 1;
1064 case RSH_COMMAND_OPTION
:
1065 rsh_command_option
= optarg
;
1070 backup_suffix_string
= optarg
;
1073 case USE_COMPRESS_PROGRAM_OPTION
:
1074 set_use_compress_program_option (optarg
);
1077 case VOLNO_FILE_OPTION
:
1078 volno_file_option
= optarg
;
1081 case WILDCARDS_OPTION
:
1082 exclude_options
|= EXCLUDE_WILDCARDS
;
1085 case WILDCARDS_MATCH_SLASH_OPTION
:
1086 exclude_options
&= ~ FNM_FILE_NAME
;
1098 #ifdef DEVICE_PREFIX
1100 int device
= optchar
- '0';
1102 static char buf
[sizeof DEVICE_PREFIX
+ 10];
1105 density
= getopt_long (argc
, argv
, "lmh", 0, 0);
1106 strcpy (buf
, DEVICE_PREFIX
);
1107 cursor
= buf
+ strlen (buf
);
1109 #ifdef DENSITY_LETTER
1111 sprintf (cursor
, "%d%c", device
, density
);
1113 #else /* not DENSITY_LETTER */
1140 usage (TAREXIT_FAILURE
);
1142 sprintf (cursor
, "%d", device
);
1144 #endif /* not DENSITY_LETTER */
1146 if (archive_names
== allocated_archive_names
)
1148 allocated_archive_names
*= 2;
1149 archive_name_array
=
1150 xrealloc (archive_name_array
,
1151 sizeof (const char *) * allocated_archive_names
);
1153 archive_name_array
[archive_names
++] = buf
;
1155 /* FIXME: How comes this works for many archives when buf is
1160 #else /* not DEVICE_PREFIX */
1163 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1165 #endif /* not DEVICE_PREFIX */
1168 /* Handle operands after any "--" argument. */
1169 for (; optind
< argc
; optind
++)
1171 name_add (argv
[optind
]);
1175 /* Process trivial options. */
1179 printf ("tar (%s) %s\n", PACKAGE_NAME
, PACKAGE_VERSION
);
1180 printf (_("Copyright (C) %d Free Software Foundation, Inc.\n"), 2003);
1182 This program comes with NO WARRANTY, to the extent permitted by law.\n\
1183 You may redistribute it under the terms of the GNU General Public License;\n\
1184 see the file named COPYING for details."));
1186 puts (_("Written by John Gilmore and Jay Fenlason."));
1188 exit (TAREXIT_SUCCESS
);
1192 usage (TAREXIT_SUCCESS
);
1194 /* Derive option values and check option consistency. */
1196 if (archive_format
== DEFAULT_FORMAT
)
1198 #if OLDGNU_COMPATIBILITY
1199 archive_format
= OLDGNU_FORMAT
;
1201 archive_format
= GNU_FORMAT
;
1205 if (archive_format
== GNU_FORMAT
&& getenv ("POSIXLY_CORRECT"))
1206 archive_format
= POSIX_FORMAT
;
1208 if ((volume_label_option
1209 || incremental_option
|| multi_volume_option
|| sparse_option
)
1210 && archive_format
!= OLDGNU_FORMAT
&& archive_format
!= GNU_FORMAT
)
1212 _("GNU features wanted on incompatible archive format")));
1214 if (archive_names
== 0)
1216 /* If no archive file name given, try TAPE from the environment, or
1217 else, DEFAULT_ARCHIVE from the configuration process. */
1220 archive_name_array
[0] = getenv ("TAPE");
1221 if (! archive_name_array
[0])
1222 archive_name_array
[0] = DEFAULT_ARCHIVE
;
1225 /* Allow multiple archives only with `-M'. */
1227 if (archive_names
> 1 && !multi_volume_option
)
1229 _("Multiple archive files requires `-M' option")));
1231 if (listed_incremental_option
1232 && newer_mtime_option
!= TYPE_MINIMUM (time_t))
1234 _("Cannot combine --listed-incremental with --newer")));
1236 if (volume_label_option
)
1238 size_t volume_label_max_len
=
1239 (sizeof current_header
->header
.name
1240 - 1 /* for trailing '\0' */
1241 - (multi_volume_option
1242 ? (sizeof " Volume "
1243 - 1 /* for null at end of " Volume " */
1244 + INT_STRLEN_BOUND (int) /* for volume number */
1245 - 1 /* for sign, as 0 <= volno */)
1247 if (volume_label_max_len
< strlen (volume_label_option
))
1249 _("%s: Volume label is too long (limit is %lu bytes)"),
1250 quotearg_colon (volume_label_option
),
1251 (unsigned long) volume_label_max_len
));
1254 /* If ready to unlink hierarchies, so we are for simpler files. */
1255 if (recursive_unlink_option
)
1256 old_files_option
= UNLINK_FIRST_OLD_FILES
;
1258 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1259 explicit or implied, is used correctly. */
1261 switch (subcommand_option
)
1263 case CREATE_SUBCOMMAND
:
1264 if (input_files
== 0 && !files_from_option
)
1266 _("Cowardly refusing to create an empty archive")));
1269 case EXTRACT_SUBCOMMAND
:
1270 case LIST_SUBCOMMAND
:
1271 case DIFF_SUBCOMMAND
:
1272 for (archive_name_cursor
= archive_name_array
;
1273 archive_name_cursor
< archive_name_array
+ archive_names
;
1274 archive_name_cursor
++)
1275 if (!strcmp (*archive_name_cursor
, "-"))
1276 request_stdin ("-f");
1279 case CAT_SUBCOMMAND
:
1280 case UPDATE_SUBCOMMAND
:
1281 case APPEND_SUBCOMMAND
:
1282 for (archive_name_cursor
= archive_name_array
;
1283 archive_name_cursor
< archive_name_array
+ archive_names
;
1284 archive_name_cursor
++)
1285 if (!strcmp (*archive_name_cursor
, "-"))
1287 _("Options `-Aru' are incompatible with `-f -'")));
1293 archive_name_cursor
= archive_name_array
;
1295 /* Prepare for generating backup names. */
1297 if (backup_suffix_string
)
1298 simple_backup_suffix
= xstrdup (backup_suffix_string
);
1301 backup_type
= xget_version ("--backup", version_control_string
);
1303 if (verbose_option
&& textual_date_option
)
1305 char const *treated_as
= tartime (newer_mtime_option
);
1306 if (strcmp (textual_date_option
, treated_as
) != 0)
1307 WARN ((0, 0, _("Treating date `%s' as %s"),
1308 textual_date_option
, treated_as
));
1314 /* Main routine for tar. */
1316 main (int argc
, char **argv
)
1318 #if HAVE_CLOCK_GETTIME
1319 if (clock_gettime (CLOCK_REALTIME
, &start_timespec
) != 0)
1321 start_time
= time (0);
1322 program_name
= argv
[0];
1323 setlocale (LC_ALL
, "");
1324 bindtextdomain (PACKAGE
, LOCALEDIR
);
1325 textdomain (PACKAGE
);
1327 exit_status
= TAREXIT_SUCCESS
;
1328 filename_terminator
= '\n';
1329 set_quoting_style (0, escape_quoting_style
);
1331 /* Pre-allocate a few structures. */
1333 allocated_archive_names
= 10;
1334 archive_name_array
=
1335 xmalloc (sizeof (const char *) * allocated_archive_names
);
1339 /* System V fork+wait does not work if SIGCHLD is ignored. */
1340 signal (SIGCHLD
, SIG_DFL
);
1345 /* Decode options. */
1347 decode_options (argc
, argv
);
1348 name_init (argc
, argv
);
1350 /* Main command execution. */
1352 if (volno_file_option
)
1353 init_volume_number ();
1355 switch (subcommand_option
)
1357 case UNKNOWN_SUBCOMMAND
:
1359 _("You must specify one of the `-Acdtrux' options")));
1361 case CAT_SUBCOMMAND
:
1362 case UPDATE_SUBCOMMAND
:
1363 case APPEND_SUBCOMMAND
:
1367 case DELETE_SUBCOMMAND
:
1368 delete_archive_members ();
1371 case CREATE_SUBCOMMAND
:
1376 print_total_written ();
1379 case EXTRACT_SUBCOMMAND
:
1381 read_and (extract_archive
);
1383 /* FIXME: should extract_finish () even if an ordinary signal is
1389 case LIST_SUBCOMMAND
:
1390 read_and (list_archive
);
1393 case DIFF_SUBCOMMAND
:
1395 read_and (diff_archive
);
1399 if (volno_file_option
)
1400 closeout_volume_number ();
1402 /* Dispose of allocated memory, and return. */
1404 free (archive_name_array
);
1407 if (stdlis
!= stderr
&& (ferror (stdlis
) || fclose (stdlis
) != 0))
1408 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1409 if (exit_status
== TAREXIT_FAILURE
)
1410 error (0, 0, _("Error exit delayed from previous errors"));
1411 if (ferror (stderr
) || fclose (stderr
) != 0)
1412 exit_status
= TAREXIT_FAILURE
;