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_FORMAT
48 # define DEFAULT_ARCHIVE_FORMAT GNU_FORMAT
51 #ifndef DEFAULT_ARCHIVE
52 # define DEFAULT_ARCHIVE "tar.out"
55 #ifndef DEFAULT_BLOCKING
56 # define DEFAULT_BLOCKING 20
59 static void usage (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';
120 static struct fmttab
{
122 enum archive_format fmt
;
125 { "oldgnu", OLDGNU_FORMAT
},
126 { "posix", POSIX_FORMAT
},
127 #if 0 /* not fully supported yet */
128 { "star", STAR_FORMAT
},
130 { "gnu", GNU_FORMAT
},
135 set_archive_format (char const *name
)
137 struct fmttab
const *p
;
139 for (p
= fmttab
; strcmp (p
->name
, name
) != 0; )
141 USAGE_ERROR ((0, 0, _("%s: Invalid archive format"),
142 quotearg_colon (name
)));
144 if (archive_format
!= DEFAULT_FORMAT
&& archive_format
!= p
->fmt
)
145 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
147 archive_format
= p
->fmt
;
151 archive_format_string (enum archive_format fmt
)
153 struct fmttab
const *p
;
155 for (p
= fmttab
; p
->name
; p
++)
161 #define FORMAT_MASK(n) (1<<(n))
164 assert_format(unsigned fmt_mask
)
166 if ((FORMAT_MASK(archive_format
) & fmt_mask
) == 0)
168 _("GNU features wanted on incompatible archive format")));
175 /* For long options that unconditionally set a single flag, we have getopt
176 do it. For the others, we share the code for the equivalent short
177 named option, the name of which is stored in the otherwise-unused `val'
178 field of the `struct option'; for long options that have no equivalent
179 short option, we use non-characters as pseudo short options,
180 starting at CHAR_MAX + 1 and going upwards. */
184 ANCHORED_OPTION
= CHAR_MAX
+ 1,
185 ATIME_PRESERVE_OPTION
,
195 IGNORE_FAILED_READ_OPTION
,
200 NO_IGNORE_CASE_OPTION
,
201 NO_OVERWRITE_DIR_OPTION
,
203 NO_WILDCARDS_MATCH_SLASH_OPTION
,
205 NUMERIC_OWNER_OPTION
,
211 RECURSIVE_UNLINK_OPTION
,
214 SHOW_OMITTED_DIRS_OPTION
,
218 USE_COMPRESS_PROGRAM_OPTION
,
221 WILDCARDS_MATCH_SLASH_OPTION
,
224 /* If nonzero, display usage information and exit. */
225 static int show_help
;
227 /* If nonzero, print the version on standard output and exit. */
228 static int show_version
;
230 /* If nonzero, stop processing when all the files from the namelist
232 static int first_copy_option
;
234 static struct option long_options
[] =
236 {"absolute-names", no_argument
, 0, 'P'},
237 {"after-date", required_argument
, 0, 'N'},
238 {"anchored", no_argument
, 0, ANCHORED_OPTION
},
239 {"append", no_argument
, 0, 'r'},
240 {"atime-preserve", no_argument
, 0, ATIME_PRESERVE_OPTION
},
241 {"backup", optional_argument
, 0, BACKUP_OPTION
},
242 {"block-number", no_argument
, 0, 'R'},
243 {"blocking-factor", required_argument
, 0, 'b'},
244 {"bzip2", no_argument
, 0, 'j'},
245 {"catenate", no_argument
, 0, 'A'},
246 {"checkpoint", no_argument
, 0, CHECKPOINT_OPTION
},
247 {"check-links", no_argument
, &check_links_option
, 1},
248 {"compare", no_argument
, 0, 'd'},
249 {"compress", no_argument
, 0, 'Z'},
250 {"concatenate", no_argument
, 0, 'A'},
251 {"confirmation", no_argument
, 0, 'w'},
252 /* FIXME: --selective as a synonym for --confirmation? */
253 {"create", no_argument
, 0, 'c'},
254 {"delete", no_argument
, 0, DELETE_OPTION
},
255 {"dereference", no_argument
, 0, 'h'},
256 {"diff", no_argument
, 0, 'd'},
257 {"directory", required_argument
, 0, 'C'},
258 {"exclude", required_argument
, 0, EXCLUDE_OPTION
},
259 {"exclude-from", required_argument
, 0, 'X'},
260 {"extract", no_argument
, 0, 'x'},
261 {"file", required_argument
, 0, 'f'},
262 {"files-from", required_argument
, 0, 'T'},
263 {"first-copy", no_argument
, &first_copy_option
, 1},
264 {"force-local", no_argument
, 0, FORCE_LOCAL_OPTION
},
265 {"format", required_argument
, 0, FORMAT_OPTION
},
266 {"get", no_argument
, 0, 'x'},
267 {"group", required_argument
, 0, GROUP_OPTION
},
268 {"gunzip", no_argument
, 0, 'z'},
269 {"gzip", no_argument
, 0, 'z'},
270 {"help", no_argument
, &show_help
, 1},
271 {"ignore-case", no_argument
, 0, IGNORE_CASE_OPTION
},
272 {"ignore-failed-read", no_argument
, 0, IGNORE_FAILED_READ_OPTION
},
273 {"ignore-zeros", no_argument
, 0, 'i'},
274 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
275 {"incremental", no_argument
, 0, 'G'},
276 {"index-file", required_argument
, 0, INDEX_FILE_OPTION
},
277 {"info-script", required_argument
, 0, 'F'},
278 {"interactive", no_argument
, 0, 'w'},
279 {"keep-old-files", no_argument
, 0, 'k'},
280 {"label", required_argument
, 0, 'V'},
281 {"list", no_argument
, 0, 't'},
282 {"listed-incremental", required_argument
, 0, 'g'},
283 {"mode", required_argument
, 0, MODE_OPTION
},
284 {"multi-volume", no_argument
, 0, 'M'},
285 {"new-volume-script", required_argument
, 0, 'F'},
286 {"newer", required_argument
, 0, 'N'},
287 {"newer-mtime", required_argument
, 0, NEWER_MTIME_OPTION
},
288 {"null", no_argument
, 0, NULL_OPTION
},
289 {"no-anchored", no_argument
, 0, NO_ANCHORED_OPTION
},
290 {"no-ignore-case", no_argument
, 0, NO_IGNORE_CASE_OPTION
},
291 {"no-overwrite-dir", no_argument
, 0, NO_OVERWRITE_DIR_OPTION
},
292 {"no-wildcards", no_argument
, 0, NO_WILDCARDS_OPTION
},
293 {"no-wildcards-match-slash", no_argument
, 0, NO_WILDCARDS_MATCH_SLASH_OPTION
},
294 {"no-recursion", no_argument
, &recursion_option
, 0},
295 {"no-same-owner", no_argument
, &same_owner_option
, -1},
296 {"no-same-permissions", no_argument
, &same_permissions_option
, -1},
297 {"numeric-owner", no_argument
, 0, NUMERIC_OWNER_OPTION
},
298 {"old-archive", no_argument
, 0, 'o'},
299 {"one-file-system", no_argument
, 0, 'l'},
300 {"overwrite", no_argument
, 0, OVERWRITE_OPTION
},
301 {"owner", required_argument
, 0, OWNER_OPTION
},
302 {"portability", no_argument
, 0, 'o'},
303 {"posix", no_argument
, 0, POSIX_OPTION
},
304 {"preserve", no_argument
, 0, PRESERVE_OPTION
},
305 {"preserve-order", no_argument
, 0, 's'},
306 {"preserve-permissions", no_argument
, 0, 'p'},
307 {"recursion", no_argument
, &recursion_option
, FNM_LEADING_DIR
},
308 {"recursive-unlink", no_argument
, 0, RECURSIVE_UNLINK_OPTION
},
309 {"read-full-records", no_argument
, 0, 'B'},
310 /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
311 {"record-size", required_argument
, 0, RECORD_SIZE_OPTION
},
312 {"remove-files", no_argument
, 0, REMOVE_FILES_OPTION
},
313 {"rsh-command", required_argument
, 0, RSH_COMMAND_OPTION
},
314 {"same-order", no_argument
, 0, 's'},
315 {"same-owner", no_argument
, &same_owner_option
, 1},
316 {"same-permissions", no_argument
, 0, 'p'},
317 {"show-omitted-dirs", no_argument
, 0, SHOW_OMITTED_DIRS_OPTION
},
318 {"sparse", no_argument
, 0, 'S'},
319 {"starting-file", required_argument
, 0, 'K'},
320 {"strip-path", required_argument
, 0, STRIP_PATH_OPTION
},
321 {"suffix", required_argument
, 0, SUFFIX_OPTION
},
322 {"tape-length", required_argument
, 0, 'L'},
323 {"to-stdout", no_argument
, 0, 'O'},
324 {"totals", no_argument
, 0, TOTALS_OPTION
},
325 {"touch", no_argument
, 0, 'm'},
326 {"uncompress", no_argument
, 0, 'Z'},
327 {"ungzip", no_argument
, 0, 'z'},
328 {"unlink-first", no_argument
, 0, 'U'},
329 {"update", no_argument
, 0, 'u'},
330 {"use-compress-program", required_argument
, 0, USE_COMPRESS_PROGRAM_OPTION
},
331 {"verbose", no_argument
, 0, 'v'},
332 {"verify", no_argument
, 0, 'W'},
333 {"version", no_argument
, &show_version
, 1},
334 {"volno-file", required_argument
, 0, VOLNO_FILE_OPTION
},
335 {"wildcards", no_argument
, 0, WILDCARDS_OPTION
},
336 {"wildcards-match-slash", no_argument
, 0, WILDCARDS_MATCH_SLASH_OPTION
},
341 /* Print a usage message and exit with STATUS. */
345 if (status
!= TAREXIT_SUCCESS
)
346 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
351 GNU `tar' saves many files together into a single tape or disk archive, and\n\
352 can restore individual files from the archive.\n"),
354 printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
357 %s -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
358 %s -tvf archive.tar # List all files in archive.tar verbosely.\n\
359 %s -xf archive.tar # Extract all files from archive.tar.\n"),
360 program_name
, program_name
, program_name
, program_name
);
363 If a long option shows an argument as mandatory, then it is mandatory\n\
364 for the equivalent short option also. Similarly for optional arguments.\n"),
368 Main operation mode:\n\
369 -t, --list list the contents of an archive\n\
370 -x, --extract, --get extract files from an archive\n\
371 -c, --create create a new archive\n\
372 -d, --diff, --compare find differences between archive and file system\n\
373 -r, --append append files to the end of an archive\n\
374 -u, --update only append files newer than copy in archive\n\
375 -A, --catenate append tar files to an archive\n\
376 --concatenate same as -A\n\
377 --delete delete from the archive (not on mag tapes!)\n"),
381 Operation modifiers:\n\
382 -W, --verify attempt to verify the archive after writing it\n\
383 --remove-files remove files after adding them to the archive\n\
384 -k, --keep-old-files don't replace existing files when extracting\n\
385 --overwrite overwrite existing files when extracting\n\
386 --no-overwrite-dir preserve metadata of existing directories\n\
387 -U, --unlink-first remove each file prior to extracting over it\n\
388 --recursive-unlink empty hierarchies prior to extracting directory\n\
389 -S, --sparse handle sparse files efficiently\n\
390 -O, --to-stdout extract files to standard output\n\
391 -G, --incremental handle old GNU-format incremental backup\n\
392 -g, --listed-incremental=FILE\n\
393 handle new GNU-format incremental backup\n\
394 --ignore-failed-read do not exit with nonzero on unreadable files\n\
395 --first-copy process only the first copy of each file in the\
400 Handling of file attributes:\n\
401 --owner=NAME force NAME as owner for added files\n\
402 --group=NAME force NAME as group for added files\n\
403 --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
404 --atime-preserve don't change access times on dumped files\n\
405 -m, --modification-time don't extract file modified time\n\
406 --same-owner try extracting files with the same ownership\n\
407 --no-same-owner extract files as yourself\n\
408 --numeric-owner always use numbers for user/group names\n\
409 -p, --same-permissions extract permissions information\n\
410 --no-same-permissions do not extract permissions information\n\
411 --preserve-permissions same as -p\n\
412 -s, --same-order sort names to extract to match archive\n\
413 --preserve-order same as -s\n\
414 --preserve same as both -p and -s\n"),
418 Device selection and switching:\n\
419 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
420 --force-local archive file is local even if has a colon\n\
421 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
422 -[0-7][lmh] specify drive and density\n\
423 -M, --multi-volume create/list/extract multi-volume archive\n\
424 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
425 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
426 --new-volume-script=FILE same as -F FILE\n\
427 --volno-file=FILE use/update the volume number in FILE\n"),
432 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
433 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
434 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
435 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
439 Archive format selection:\n\
440 --format=FMTNAME create archive of the given format.\n\
441 FMTNAME is one of the following:\n\
442 v7 old V7 tar format\n\
443 oldgnu GNU format as per tar <= 1.12\n\
444 posix POSIX 1003.1-2001 tar format\n\
446 --old-archive, --portability same as --format=v7\n\
447 --posix same as --format=posix\n\
448 -V, --label=NAME create archive with volume name NAME\n\
449 PATTERN at list/extract time, a globbing PATTERN\n\
450 -j, --bzip2 filter the archive through bzip2\n\
451 -z, --gzip, --ungzip filter the archive through gzip\n\
452 -Z, --compress, --uncompress filter the archive through compress\n\
453 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
457 Local file selection:\n\
458 -C, --directory=DIR change to directory DIR\n\
459 -T, --files-from=NAME get names to extract or create from file NAME\n\
460 --null -T reads null-terminated names, disable -C\n\
461 --exclude=PATTERN exclude files, given as a PATTERN\n\
462 -X, --exclude-from=FILE exclude patterns listed in FILE\n\
463 --anchored exclude patterns match file name start (default)\n\
464 --no-anchored exclude patterns match after any /\n\
465 --ignore-case exclusion ignores case\n\
466 --no-ignore-case exclusion is case sensitive (default)\n\
467 --wildcards exclude patterns use wildcards (default)\n\
468 --no-wildcards exclude patterns are plain strings\n\
469 --wildcards-match-slash exclude pattern wildcards match '/' (default)\n\
470 --no-wildcards-match-slash exclude pattern wildcards do not match '/'\n\
471 -P, --absolute-names don't strip leading `/'s from file names\n\
472 -h, --dereference dump instead the files symlinks point to\n\
473 --no-recursion avoid descending automatically in directories\n\
474 -l, --one-file-system stay in local file system when creating archive\n\
475 -K, --starting-file=NAME begin at file NAME in the archive\n\
476 --strip-path=NUM strip NUM leading components from file names\n\
477 before extraction\n"),
481 -N, --newer=DATE only store files newer than DATE\n\
482 --newer-mtime=DATE compare date and time when data changed only\n\
483 --after-date=DATE same as -N\n"),
487 --backup[=CONTROL] backup before removal, choose version control\n\
488 --suffix=SUFFIX backup before removal, override usual suffix\n"),
492 Informative output:\n\
493 --help print this help, then exit\n\
494 --version print tar program version number, then exit\n\
495 -v, --verbose verbosely list files processed\n\
496 --checkpoint print directory names while reading the archive\n\
497 --check-links print a message if not all links are dumped\n\
498 --totals print total bytes written while creating archive\n\
499 --index-file=FILE send verbose output to FILE\n\
500 -R, --block-number show block number within archive with each message\n\
501 -w, --interactive ask for confirmation for every action\n\
502 --confirmation same as -w\n"),
506 Compatibility options:\n\
507 -o when creating, same as --old-archive\n\
508 when extracting, same as --no-same-owner\n"),
513 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
514 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
516 t, numbered make numbered backups\n\
517 nil, existing numbered if numbered backups exist, simple otherwise\n\
518 never, simple always make simple backups\n"),
522 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; DATE may be a textual date\n\
523 or a file name starting with `/' or `.', in which case the file's date is used.\n\
524 *This* `tar' defaults to `--format=%s -f%s -b%d'.\n"),
525 archive_format_string (DEFAULT_ARCHIVE_FORMAT
),
526 DEFAULT_ARCHIVE
, DEFAULT_BLOCKING
);
527 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
532 /* Parse the options for tar. */
534 /* Available option letters are DEHIJQY and aenqy. Some are reserved:
536 e exit immediately with a nonzero exit status if unexpected errors occur
537 E use extended headers (draft POSIX headers, that is)
538 I same as T (for compatibility with Solaris tar)
539 n the archive is quickly seekable, so don't worry about random seeks
540 q stop after extracting the first occurrence of the named file
541 y per-file gzip compression
542 Y per-block gzip compression */
544 #define OPTION_STRING \
545 "-01234567ABC:F:GIK:L:MN:OPRST:UV:WX:Zb:cdf:g:hijklmoprstuvwxyz"
548 set_subcommand_option (enum subcommand subcommand
)
550 if (subcommand_option
!= UNKNOWN_SUBCOMMAND
551 && subcommand_option
!= subcommand
)
553 _("You may not specify more than one `-Acdtrux' option")));
555 subcommand_option
= subcommand
;
559 set_use_compress_program_option (const char *string
)
561 if (use_compress_program_option
&& strcmp (use_compress_program_option
, string
) != 0)
562 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
564 use_compress_program_option
= string
;
568 decode_options (int argc
, char **argv
)
570 int optchar
; /* option letter */
571 int input_files
; /* number of input files */
572 char const *textual_date_option
= 0;
573 char const *backup_suffix_string
;
574 char const *version_control_string
= 0;
575 int exclude_options
= EXCLUDE_WILDCARDS
;
578 /* Set some default option values. */
580 subcommand_option
= UNKNOWN_SUBCOMMAND
;
581 archive_format
= DEFAULT_FORMAT
;
582 blocking_factor
= DEFAULT_BLOCKING
;
583 record_size
= DEFAULT_BLOCKING
* BLOCKSIZE
;
584 excluded
= new_exclude ();
585 newer_mtime_option
= TYPE_MINIMUM (time_t);
586 recursion_option
= FNM_LEADING_DIR
;
591 backup_suffix_string
= getenv ("SIMPLE_BACKUP_SUFFIX");
593 /* Convert old-style tar call by exploding option element and rearranging
594 options accordingly. */
596 if (argc
> 1 && argv
[1][0] != '-')
598 int new_argc
; /* argc value for rearranged arguments */
599 char **new_argv
; /* argv value for rearranged arguments */
600 char *const *in
; /* cursor into original argv */
601 char **out
; /* cursor into rearranged argv */
602 const char *letter
; /* cursor into old option letters */
603 char buffer
[3]; /* constructed option buffer */
604 const char *cursor
; /* cursor in OPTION_STRING */
606 /* Initialize a constructed option. */
611 /* Allocate a new argument array, and copy program name in it. */
613 new_argc
= argc
- 1 + strlen (argv
[1]);
614 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (char *));
619 /* Copy each old letter option as a separate option, and have the
620 corresponding argument moved next to it. */
622 for (letter
= *in
++; *letter
; letter
++)
625 *out
++ = xstrdup (buffer
);
626 cursor
= strchr (OPTION_STRING
, *letter
);
627 if (cursor
&& cursor
[1] == ':')
629 if (in
< argv
+ argc
)
632 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
637 /* Copy all remaining options. */
639 while (in
< argv
+ argc
)
643 /* Replace the old option list by the new one. */
649 /* Parse all options and non-options as they appear. */
653 prepend_default_options (getenv ("TAR_OPTIONS"), &argc
, &argv
);
655 while (optchar
= getopt_long (argc
, argv
, OPTION_STRING
, long_options
, 0),
660 usage (TAREXIT_FAILURE
);
666 /* File name or non-parsed option, because of RETURN_IN_ORDER
667 ordering triggered by the leading dash in OPTION_STRING. */
674 set_subcommand_option (CAT_SUBCOMMAND
);
680 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
681 && u
== (blocking_factor
= u
)
682 && 0 < blocking_factor
683 && u
== (record_size
= u
* BLOCKSIZE
) / BLOCKSIZE
))
684 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
685 _("Invalid blocking factor")));
690 /* Try to reblock input records. For reading 4.2BSD pipes. */
692 /* It would surely make sense to exchange -B and -R, but it seems
693 that -B has been used for a long while in Sun tar ans most
694 BSD-derived systems. This is a consequence of the block/record
695 terminology confusion. */
697 read_full_records_option
= true;
701 set_subcommand_option (CREATE_SUBCOMMAND
);
710 set_subcommand_option (DIFF_SUBCOMMAND
);
714 if (archive_names
== allocated_archive_names
)
716 allocated_archive_names
*= 2;
718 xrealloc (archive_name_array
,
719 sizeof (const char *) * allocated_archive_names
);
721 archive_name_array
[archive_names
++] = optarg
;
725 /* Since -F is only useful with -M, make it implied. Run this
726 script at the end of each tape. */
728 info_script_option
= optarg
;
729 multi_volume_option
= true;
733 listed_incremental_option
= optarg
;
734 after_date_option
= true;
738 /* We are making an incremental dump (FIXME: are we?); save
739 directories at the beginning of the archive, and include in each
740 directory its contents. */
742 incremental_option
= true;
746 /* Follow symbolic links. */
747 dereference_option
= true;
751 /* Ignore zero blocks (eofs). This can't be the default,
752 because Unix tar writes two blocks of zeros, then pads out
753 the record with garbage. */
755 ignore_zeros_option
= true;
760 _("Warning: the -I option is not supported;"
761 " perhaps you meant -j or -T?")));
765 set_use_compress_program_option ("bzip2");
769 /* Don't replace existing files. */
770 old_files_option
= KEEP_OLD_FILES
;
774 starting_file_option
= true;
779 /* When dumping directories, don't dump files/subdirectories
780 that are on other filesystems. */
782 one_file_system_option
= true;
788 if (xstrtoumax (optarg
, 0, 10, &u
, "") != LONGINT_OK
)
789 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
790 _("Invalid tape length")));
791 tape_length_option
= 1024 * (tarlong
) u
;
792 multi_volume_option
= true;
801 /* Make multivolume archive: when we can't write any more into
802 the archive, re-open it, and continue writing. */
804 multi_volume_option
= true;
809 after_date_option
= true;
812 case NEWER_MTIME_OPTION
:
813 if (newer_mtime_option
!= TYPE_MINIMUM (time_t))
814 USAGE_ERROR ((0, 0, _("More than one threshold date")));
816 if (FILESYSTEM_PREFIX_LEN (optarg
) != 0
821 if (deref_stat (dereference_option
, optarg
, &st
) != 0)
824 USAGE_ERROR ((0, 0, _("Date file not found")));
826 newer_mtime_option
= st
.st_mtime
;
830 newer_mtime_option
= get_date (optarg
, 0);
831 if (newer_mtime_option
== (time_t) -1)
832 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
833 tartime (newer_mtime_option
), quote (optarg
)));
835 textual_date_option
= optarg
;
839 #endif /* not MSDOS */
846 to_stdout_option
= true;
850 same_permissions_option
= true;
854 absolute_names_option
= true;
858 set_subcommand_option (APPEND_SUBCOMMAND
);
862 /* Print block numbers for debugging bad tar archives. */
864 /* It would surely make sense to exchange -B and -R, but it seems
865 that -B has been used for a long while in Sun tar ans most
866 BSD-derived systems. This is a consequence of the block/record
867 terminology confusion. */
869 block_number_option
= true;
873 /* Names to extr are sorted. */
875 same_order_option
= true;
879 sparse_option
= true;
883 set_subcommand_option (LIST_SUBCOMMAND
);
888 files_from_option
= optarg
;
892 set_subcommand_option (UPDATE_SUBCOMMAND
);
896 old_files_option
= UNLINK_FIRST_OLD_FILES
;
904 volume_label_option
= optarg
;
908 interactive_option
= true;
912 verify_option
= true;
916 set_subcommand_option (EXTRACT_SUBCOMMAND
);
920 if (add_exclude_file (add_exclude
, excluded
, optarg
,
921 exclude_options
| recursion_option
, '\n')
925 FATAL_ERROR ((0, e
, "%s", quotearg_colon (optarg
)));
931 _("Warning: the -y option is not supported;"
932 " perhaps you meant -j?")));
936 set_use_compress_program_option ("gzip");
940 set_use_compress_program_option ("compress");
943 case ANCHORED_OPTION
:
944 exclude_options
|= EXCLUDE_ANCHORED
;
947 case ATIME_PRESERVE_OPTION
:
948 atime_preserve_option
= true;
951 case CHECKPOINT_OPTION
:
952 checkpoint_option
= true;
956 backup_option
= true;
958 version_control_string
= optarg
;
962 set_subcommand_option (DELETE_SUBCOMMAND
);
966 add_exclude (excluded
, optarg
, exclude_options
| recursion_option
);
969 case FORCE_LOCAL_OPTION
:
970 force_local_option
= true;
974 set_archive_format (optarg
);
977 case INDEX_FILE_OPTION
:
978 index_file_name
= optarg
;
981 case IGNORE_CASE_OPTION
:
982 exclude_options
|= FNM_CASEFOLD
;
985 case IGNORE_FAILED_READ_OPTION
:
986 ignore_failed_read_option
= true;
990 if (! (strlen (optarg
) < GNAME_FIELD_SIZE
991 && gname_to_gid (optarg
, &group_option
)))
994 if (xstrtoumax (optarg
, 0, 10, &g
, "") == LONGINT_OK
998 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
999 _("%s: Invalid group")));
1005 = mode_compile (optarg
,
1006 MODE_MASK_EQUALS
| MODE_MASK_PLUS
| MODE_MASK_MINUS
);
1007 if (mode_option
== MODE_INVALID
)
1008 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
1009 if (mode_option
== MODE_MEMORY_EXHAUSTED
)
1013 case NO_ANCHORED_OPTION
:
1014 exclude_options
&= ~ EXCLUDE_ANCHORED
;
1017 case NO_IGNORE_CASE_OPTION
:
1018 exclude_options
&= ~ FNM_CASEFOLD
;
1021 case NO_OVERWRITE_DIR_OPTION
:
1022 old_files_option
= NO_OVERWRITE_DIR_OLD_FILES
;
1025 case NO_WILDCARDS_OPTION
:
1026 exclude_options
&= ~ EXCLUDE_WILDCARDS
;
1029 case NO_WILDCARDS_MATCH_SLASH_OPTION
:
1030 exclude_options
|= FNM_FILE_NAME
;
1034 filename_terminator
= '\0';
1037 case NUMERIC_OWNER_OPTION
:
1038 numeric_owner_option
= true;
1041 case OVERWRITE_OPTION
:
1042 old_files_option
= OVERWRITE_OLD_FILES
;
1046 if (! (strlen (optarg
) < UNAME_FIELD_SIZE
1047 && uname_to_uid (optarg
, &owner_option
)))
1050 if (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
1054 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1055 _("Invalid owner")));
1060 set_archive_format ("posix");
1063 case PRESERVE_OPTION
:
1064 same_permissions_option
= true;
1065 same_order_option
= true;
1068 case RECORD_SIZE_OPTION
:
1071 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
1072 && u
== (size_t) u
))
1073 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1074 _("Invalid record size")));
1076 if (record_size
% BLOCKSIZE
!= 0)
1077 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
1079 blocking_factor
= record_size
/ BLOCKSIZE
;
1083 case RECURSIVE_UNLINK_OPTION
:
1084 recursive_unlink_option
= true;
1087 case REMOVE_FILES_OPTION
:
1088 remove_files_option
= true;
1091 case RSH_COMMAND_OPTION
:
1092 rsh_command_option
= optarg
;
1095 case STRIP_PATH_OPTION
:
1098 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
1099 && u
== (size_t) u
))
1100 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1101 _("Invalid number of elements")));
1102 strip_path_elements
= u
;
1107 backup_option
= true;
1108 backup_suffix_string
= optarg
;
1112 totals_option
= true;
1115 case USE_COMPRESS_PROGRAM_OPTION
:
1116 set_use_compress_program_option (optarg
);
1119 case VOLNO_FILE_OPTION
:
1120 volno_file_option
= optarg
;
1123 case WILDCARDS_OPTION
:
1124 exclude_options
|= EXCLUDE_WILDCARDS
;
1127 case WILDCARDS_MATCH_SLASH_OPTION
:
1128 exclude_options
&= ~ FNM_FILE_NAME
;
1140 #ifdef DEVICE_PREFIX
1142 int device
= optchar
- '0';
1144 static char buf
[sizeof DEVICE_PREFIX
+ 10];
1147 density
= getopt_long (argc
, argv
, "lmh", 0, 0);
1148 strcpy (buf
, DEVICE_PREFIX
);
1149 cursor
= buf
+ strlen (buf
);
1151 #ifdef DENSITY_LETTER
1153 sprintf (cursor
, "%d%c", device
, density
);
1155 #else /* not DENSITY_LETTER */
1182 usage (TAREXIT_FAILURE
);
1184 sprintf (cursor
, "%d", device
);
1186 #endif /* not DENSITY_LETTER */
1188 if (archive_names
== allocated_archive_names
)
1190 allocated_archive_names
*= 2;
1191 archive_name_array
=
1192 xrealloc (archive_name_array
,
1193 sizeof (const char *) * allocated_archive_names
);
1195 archive_name_array
[archive_names
++] = strdup (buf
);
1199 #else /* not DEVICE_PREFIX */
1202 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1204 #endif /* not DEVICE_PREFIX */
1207 /* Special handling for 'o' option:
1209 GNU tar used to say "output old format".
1210 UNIX98 tar says don't chown files after extracting (we use
1211 "--no-same-owner" for this).
1213 The old GNU tar semantics is retained when used with --create
1214 option, otherwise UNIX98 semantics is assumed */
1218 if (subcommand_option
== CREATE_SUBCOMMAND
)
1220 /* GNU Tar <= 1.13 compatibility */
1221 set_archive_format ("v7");
1225 /* UNIX98 compatibility */
1226 same_owner_option
= 1;
1230 /* Handle operands after any "--" argument. */
1231 for (; optind
< argc
; optind
++)
1233 name_add (argv
[optind
]);
1237 /* Process trivial options. */
1241 printf ("tar (%s) %s\n%s\n", PACKAGE_NAME
, PACKAGE_VERSION
,
1242 "Copyright (C) 2003 Free Software Foundation, Inc.");
1244 This program comes with NO WARRANTY, to the extent permitted by law.\n\
1245 You may redistribute it under the terms of the GNU General Public License;\n\
1246 see the file named COPYING for details."));
1248 puts (_("Written by John Gilmore and Jay Fenlason."));
1250 exit (TAREXIT_SUCCESS
);
1254 usage (TAREXIT_SUCCESS
);
1256 /* Derive option values and check option consistency. */
1258 if (archive_format
== DEFAULT_FORMAT
)
1259 archive_format
= DEFAULT_ARCHIVE_FORMAT
;
1261 if (archive_format
== GNU_FORMAT
&& getenv ("POSIXLY_CORRECT"))
1262 archive_format
= POSIX_FORMAT
;
1264 if (((volume_label_option
&& subcommand_option
== CREATE_SUBCOMMAND
)
1265 || incremental_option
|| multi_volume_option
|| sparse_option
)
1266 && archive_format
!= OLDGNU_FORMAT
&& archive_format
!= GNU_FORMAT
)
1268 _("GNU features wanted on incompatible archive format")));
1270 if (volume_label_option
&& subcommand_option
== CREATE_SUBCOMMAND
)
1271 assert_format (FORMAT_MASK (OLDGNU_FORMAT
)
1272 | FORMAT_MASK (GNU_FORMAT
));
1274 if (incremental_option
1275 || multi_volume_option
1277 || subcommand_option
== CAT_SUBCOMMAND
)
1278 assert_format (FORMAT_MASK (OLDGNU_FORMAT
)
1279 | FORMAT_MASK (GNU_FORMAT
));
1281 if (first_copy_option
)
1283 if (!input_files
&& !files_from_option
)
1285 _("--first-copy is meaningless without file list")));
1286 if (subcommand_option
!= DELETE_SUBCOMMAND
1287 && subcommand_option
!= DIFF_SUBCOMMAND
1288 && subcommand_option
!= EXTRACT_SUBCOMMAND
1289 && subcommand_option
!= LIST_SUBCOMMAND
)
1291 _("--first-copy cannot be used in the requested operation mode")));
1294 if (archive_names
== 0)
1296 /* If no archive file name given, try TAPE from the environment, or
1297 else, DEFAULT_ARCHIVE from the configuration process. */
1300 archive_name_array
[0] = getenv ("TAPE");
1301 if (! archive_name_array
[0])
1302 archive_name_array
[0] = DEFAULT_ARCHIVE
;
1305 /* Allow multiple archives only with `-M'. */
1307 if (archive_names
> 1 && !multi_volume_option
)
1309 _("Multiple archive files requires `-M' option")));
1311 if (listed_incremental_option
1312 && newer_mtime_option
!= TYPE_MINIMUM (time_t))
1314 _("Cannot combine --listed-incremental with --newer")));
1316 if (volume_label_option
)
1318 size_t volume_label_max_len
=
1319 (sizeof current_header
->header
.name
1320 - 1 /* for trailing '\0' */
1321 - (multi_volume_option
1322 ? (sizeof " Volume "
1323 - 1 /* for null at end of " Volume " */
1324 + INT_STRLEN_BOUND (int) /* for volume number */
1325 - 1 /* for sign, as 0 <= volno */)
1327 if (volume_label_max_len
< strlen (volume_label_option
))
1329 ngettext ("%s: Volume label is too long (limit is %lu byte)",
1330 "%s: Volume label is too long (limit is %lu bytes)",
1331 volume_label_max_len
),
1332 quotearg_colon (volume_label_option
),
1333 (unsigned long) volume_label_max_len
));
1336 /* If ready to unlink hierarchies, so we are for simpler files. */
1337 if (recursive_unlink_option
)
1338 old_files_option
= UNLINK_FIRST_OLD_FILES
;
1340 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1341 explicit or implied, is used correctly. */
1343 switch (subcommand_option
)
1345 case CREATE_SUBCOMMAND
:
1346 if (input_files
== 0 && !files_from_option
)
1348 _("Cowardly refusing to create an empty archive")));
1351 case EXTRACT_SUBCOMMAND
:
1352 case LIST_SUBCOMMAND
:
1353 case DIFF_SUBCOMMAND
:
1354 for (archive_name_cursor
= archive_name_array
;
1355 archive_name_cursor
< archive_name_array
+ archive_names
;
1356 archive_name_cursor
++)
1357 if (!strcmp (*archive_name_cursor
, "-"))
1358 request_stdin ("-f");
1361 case CAT_SUBCOMMAND
:
1362 case UPDATE_SUBCOMMAND
:
1363 case APPEND_SUBCOMMAND
:
1364 for (archive_name_cursor
= archive_name_array
;
1365 archive_name_cursor
< archive_name_array
+ archive_names
;
1366 archive_name_cursor
++)
1367 if (!strcmp (*archive_name_cursor
, "-"))
1369 _("Options `-Aru' are incompatible with `-f -'")));
1375 archive_name_cursor
= archive_name_array
;
1377 /* Prepare for generating backup names. */
1379 if (backup_suffix_string
)
1380 simple_backup_suffix
= xstrdup (backup_suffix_string
);
1383 backup_type
= xget_version ("--backup", version_control_string
);
1385 if (verbose_option
&& textual_date_option
)
1387 char const *treated_as
= tartime (newer_mtime_option
);
1388 if (strcmp (textual_date_option
, treated_as
) != 0)
1389 WARN ((0, 0, _("Treating date `%s' as %s"),
1390 textual_date_option
, treated_as
));
1397 return first_copy_option
&& names_done ();
1403 /* Main routine for tar. */
1405 main (int argc
, char **argv
)
1407 #if HAVE_CLOCK_GETTIME
1408 if (clock_gettime (CLOCK_REALTIME
, &start_timespec
) != 0)
1410 start_time
= time (0);
1411 program_name
= argv
[0];
1412 setlocale (LC_ALL
, "");
1413 bindtextdomain (PACKAGE
, LOCALEDIR
);
1414 textdomain (PACKAGE
);
1416 exit_status
= TAREXIT_SUCCESS
;
1417 filename_terminator
= '\n';
1418 set_quoting_style (0, escape_quoting_style
);
1420 /* Pre-allocate a few structures. */
1422 allocated_archive_names
= 10;
1423 archive_name_array
=
1424 xmalloc (sizeof (const char *) * allocated_archive_names
);
1428 /* System V fork+wait does not work if SIGCHLD is ignored. */
1429 signal (SIGCHLD
, SIG_DFL
);
1434 /* Decode options. */
1436 decode_options (argc
, argv
);
1437 name_init (argc
, argv
);
1439 /* Main command execution. */
1441 if (volno_file_option
)
1442 init_volume_number ();
1444 switch (subcommand_option
)
1446 case UNKNOWN_SUBCOMMAND
:
1448 _("You must specify one of the `-Acdtrux' options")));
1450 case CAT_SUBCOMMAND
:
1451 case UPDATE_SUBCOMMAND
:
1452 case APPEND_SUBCOMMAND
:
1456 case DELETE_SUBCOMMAND
:
1457 delete_archive_members ();
1460 case CREATE_SUBCOMMAND
:
1465 print_total_written ();
1468 case EXTRACT_SUBCOMMAND
:
1470 read_and (extract_archive
);
1472 /* FIXME: should extract_finish () even if an ordinary signal is
1478 case LIST_SUBCOMMAND
:
1479 read_and (list_archive
);
1482 case DIFF_SUBCOMMAND
:
1484 read_and (diff_archive
);
1488 if (check_links_option
)
1491 if (volno_file_option
)
1492 closeout_volume_number ();
1494 /* Dispose of allocated memory, and return. */
1496 free (archive_name_array
);
1499 if (stdlis
!= stderr
&& (ferror (stdlis
) || fclose (stdlis
) != 0))
1500 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1501 if (exit_status
== TAREXIT_FAILURE
)
1502 error (0, 0, _("Error exit delayed from previous errors"));
1503 if (ferror (stderr
) || fclose (stderr
) != 0)
1504 exit_status
= TAREXIT_FAILURE
;
1509 destroy_stat (struct tar_stat_info
*st
)
1511 free (st
->orig_file_name
);
1512 free (st
->file_name
);
1513 free (st
->link_name
);
1516 memset (st
, 0, sizeof (*st
));