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 { "ustar", USTAR_FORMAT
},
127 { "posix", POSIX_FORMAT
},
128 #if 0 /* not fully supported yet */
129 { "star", STAR_FORMAT
},
131 { "gnu", GNU_FORMAT
},
136 set_archive_format (char const *name
)
138 struct fmttab
const *p
;
140 for (p
= fmttab
; strcmp (p
->name
, name
) != 0; )
142 USAGE_ERROR ((0, 0, _("%s: Invalid archive format"),
143 quotearg_colon (name
)));
145 if (archive_format
!= DEFAULT_FORMAT
&& archive_format
!= p
->fmt
)
146 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
148 archive_format
= p
->fmt
;
152 archive_format_string (enum archive_format fmt
)
154 struct fmttab
const *p
;
156 for (p
= fmttab
; p
->name
; p
++)
162 #define FORMAT_MASK(n) (1<<(n))
165 assert_format(unsigned fmt_mask
)
167 if ((FORMAT_MASK(archive_format
) & fmt_mask
) == 0)
169 _("GNU features wanted on incompatible archive format")));
176 /* For long options that unconditionally set a single flag, we have getopt
177 do it. For the others, we share the code for the equivalent short
178 named option, the name of which is stored in the otherwise-unused `val'
179 field of the `struct option'; for long options that have no equivalent
180 short option, we use non-characters as pseudo short options,
181 starting at CHAR_MAX + 1 and going upwards. */
185 ANCHORED_OPTION
= CHAR_MAX
+ 1,
186 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
,
212 RECURSIVE_UNLINK_OPTION
,
215 SHOW_OMITTED_DIRS_OPTION
,
219 USE_COMPRESS_PROGRAM_OPTION
,
222 WILDCARDS_MATCH_SLASH_OPTION
225 /* If nonzero, display usage information and exit. */
226 static int show_help
;
228 /* If nonzero, print the version on standard output and exit. */
229 static int show_version
;
231 static struct option long_options
[] =
233 {"absolute-names", no_argument
, 0, 'P'},
234 {"after-date", required_argument
, 0, 'N'},
235 {"anchored", no_argument
, 0, ANCHORED_OPTION
},
236 {"append", no_argument
, 0, 'r'},
237 {"atime-preserve", no_argument
, 0, ATIME_PRESERVE_OPTION
},
238 {"backup", optional_argument
, 0, BACKUP_OPTION
},
239 {"block-number", no_argument
, 0, 'R'},
240 {"blocking-factor", required_argument
, 0, 'b'},
241 {"bzip2", no_argument
, 0, 'j'},
242 {"catenate", no_argument
, 0, 'A'},
243 {"checkpoint", no_argument
, 0, CHECKPOINT_OPTION
},
244 {"check-links", no_argument
, &check_links_option
, 1},
245 {"compare", no_argument
, 0, 'd'},
246 {"compress", no_argument
, 0, 'Z'},
247 {"concatenate", no_argument
, 0, 'A'},
248 {"confirmation", no_argument
, 0, 'w'},
249 /* FIXME: --selective as a synonym for --confirmation? */
250 {"create", no_argument
, 0, 'c'},
251 {"delete", no_argument
, 0, DELETE_OPTION
},
252 {"dereference", no_argument
, 0, 'h'},
253 {"diff", no_argument
, 0, 'd'},
254 {"directory", required_argument
, 0, 'C'},
255 {"exclude", required_argument
, 0, EXCLUDE_OPTION
},
256 {"exclude-from", required_argument
, 0, 'X'},
257 {"extract", no_argument
, 0, 'x'},
258 {"file", required_argument
, 0, 'f'},
259 {"files-from", required_argument
, 0, 'T'},
260 {"force-local", no_argument
, 0, FORCE_LOCAL_OPTION
},
261 {"format", required_argument
, 0, FORMAT_OPTION
},
262 {"get", no_argument
, 0, 'x'},
263 {"group", required_argument
, 0, GROUP_OPTION
},
264 {"gunzip", no_argument
, 0, 'z'},
265 {"gzip", no_argument
, 0, 'z'},
266 {"help", no_argument
, &show_help
, 1},
267 {"ignore-case", no_argument
, 0, IGNORE_CASE_OPTION
},
268 {"ignore-failed-read", no_argument
, 0, IGNORE_FAILED_READ_OPTION
},
269 {"ignore-zeros", no_argument
, 0, 'i'},
270 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
271 {"incremental", no_argument
, 0, 'G'},
272 {"index-file", required_argument
, 0, INDEX_FILE_OPTION
},
273 {"info-script", required_argument
, 0, 'F'},
274 {"interactive", no_argument
, 0, 'w'},
275 {"keep-old-files", no_argument
, 0, 'k'},
276 {"label", required_argument
, 0, 'V'},
277 {"list", no_argument
, 0, 't'},
278 {"listed-incremental", required_argument
, 0, 'g'},
279 {"mode", required_argument
, 0, MODE_OPTION
},
280 {"multi-volume", no_argument
, 0, 'M'},
281 {"new-volume-script", required_argument
, 0, 'F'},
282 {"newer", required_argument
, 0, 'N'},
283 {"newer-mtime", required_argument
, 0, NEWER_MTIME_OPTION
},
284 {"null", no_argument
, 0, NULL_OPTION
},
285 {"no-anchored", no_argument
, 0, NO_ANCHORED_OPTION
},
286 {"no-ignore-case", no_argument
, 0, NO_IGNORE_CASE_OPTION
},
287 {"no-overwrite-dir", no_argument
, 0, NO_OVERWRITE_DIR_OPTION
},
288 {"no-wildcards", no_argument
, 0, NO_WILDCARDS_OPTION
},
289 {"no-wildcards-match-slash", no_argument
, 0, NO_WILDCARDS_MATCH_SLASH_OPTION
},
290 {"no-recursion", no_argument
, &recursion_option
, 0},
291 {"no-same-owner", no_argument
, &same_owner_option
, -1},
292 {"no-same-permissions", no_argument
, &same_permissions_option
, -1},
293 {"numeric-owner", no_argument
, 0, NUMERIC_OWNER_OPTION
},
294 {"occurrence", optional_argument
, 0, OCCURRENCE_OPTION
},
295 {"old-archive", no_argument
, 0, 'o'},
296 {"one-file-system", no_argument
, 0, 'l'},
297 {"overwrite", no_argument
, 0, OVERWRITE_OPTION
},
298 {"owner", required_argument
, 0, OWNER_OPTION
},
299 {"portability", no_argument
, 0, 'o'},
300 {"posix", no_argument
, 0, POSIX_OPTION
},
301 {"preserve", no_argument
, 0, PRESERVE_OPTION
},
302 {"preserve-order", no_argument
, 0, 's'},
303 {"preserve-permissions", no_argument
, 0, 'p'},
304 {"recursion", no_argument
, &recursion_option
, FNM_LEADING_DIR
},
305 {"recursive-unlink", no_argument
, 0, RECURSIVE_UNLINK_OPTION
},
306 {"read-full-records", no_argument
, 0, 'B'},
307 /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
308 {"record-size", required_argument
, 0, RECORD_SIZE_OPTION
},
309 {"remove-files", no_argument
, 0, REMOVE_FILES_OPTION
},
310 {"rsh-command", required_argument
, 0, RSH_COMMAND_OPTION
},
311 {"same-order", no_argument
, 0, 's'},
312 {"same-owner", no_argument
, &same_owner_option
, 1},
313 {"same-permissions", no_argument
, 0, 'p'},
314 {"show-omitted-dirs", no_argument
, 0, SHOW_OMITTED_DIRS_OPTION
},
315 {"sparse", no_argument
, 0, 'S'},
316 {"starting-file", required_argument
, 0, 'K'},
317 {"strip-path", required_argument
, 0, STRIP_PATH_OPTION
},
318 {"suffix", required_argument
, 0, SUFFIX_OPTION
},
319 {"tape-length", required_argument
, 0, 'L'},
320 {"to-stdout", no_argument
, 0, 'O'},
321 {"totals", no_argument
, 0, TOTALS_OPTION
},
322 {"touch", no_argument
, 0, 'm'},
323 {"uncompress", no_argument
, 0, 'Z'},
324 {"ungzip", no_argument
, 0, 'z'},
325 {"unlink-first", no_argument
, 0, 'U'},
326 {"update", no_argument
, 0, 'u'},
327 {"use-compress-program", required_argument
, 0, USE_COMPRESS_PROGRAM_OPTION
},
328 {"verbose", no_argument
, 0, 'v'},
329 {"verify", no_argument
, 0, 'W'},
330 {"version", no_argument
, &show_version
, 1},
331 {"volno-file", required_argument
, 0, VOLNO_FILE_OPTION
},
332 {"wildcards", no_argument
, 0, WILDCARDS_OPTION
},
333 {"wildcards-match-slash", no_argument
, 0, WILDCARDS_MATCH_SLASH_OPTION
},
338 /* Print a usage message and exit with STATUS. */
342 if (status
!= TAREXIT_SUCCESS
)
343 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
348 GNU `tar' saves many files together into a single tape or disk archive, and\n\
349 can restore individual files from the archive.\n"),
351 printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
354 %s -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
355 %s -tvf archive.tar # List all files in archive.tar verbosely.\n\
356 %s -xf archive.tar # Extract all files from archive.tar.\n"),
357 program_name
, program_name
, program_name
, program_name
);
360 If a long option shows an argument as mandatory, then it is mandatory\n\
361 for the equivalent short option also. Similarly for optional arguments.\n"),
365 Main operation mode:\n\
366 -t, --list list the contents of an archive\n\
367 -x, --extract, --get extract files from an archive\n\
368 -c, --create create a new archive\n\
369 -d, --diff, --compare find differences between archive and file system\n\
370 -r, --append append files to the end of an archive\n\
371 -u, --update only append files newer than copy in archive\n\
372 -A, --catenate append tar files to an archive\n\
373 --concatenate same as -A\n\
374 --delete delete from the archive (not on mag tapes!)\n"),
378 Operation modifiers:\n\
379 -W, --verify attempt to verify the archive after writing it\n\
380 --remove-files remove files after adding them to the archive\n\
381 -k, --keep-old-files don't replace existing files when extracting\n\
382 --overwrite overwrite existing files when extracting\n\
383 --no-overwrite-dir preserve metadata of existing directories\n\
384 -U, --unlink-first remove each file prior to extracting over it\n\
385 --recursive-unlink empty hierarchies prior to extracting directory\n\
386 -S, --sparse handle sparse files efficiently\n\
387 -O, --to-stdout extract files to standard output\n\
388 -G, --incremental handle old GNU-format incremental backup\n\
389 -g, --listed-incremental=FILE\n\
390 handle new GNU-format incremental backup\n\
391 --ignore-failed-read do not exit with nonzero on unreadable files\n\
392 --occurrence[=NUM] process only the NUMth occurrence of each file in\n\
393 the archive. This option is valid only in\n\
394 conjunction with one of the subcommands --delete,\n\
395 --diff, --extract or --list and when a list of\n\
396 files is given either on the command line or\n\
398 NUM defaults to 1.\n"),
402 Handling of file attributes:\n\
403 --owner=NAME force NAME as owner for added files\n\
404 --group=NAME force NAME as group for added files\n\
405 --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
406 --atime-preserve don't change access times on dumped files\n\
407 -m, --modification-time don't extract file modified time\n\
408 --same-owner try extracting files with the same ownership\n\
409 --no-same-owner extract files as yourself\n\
410 --numeric-owner always use numbers for user/group names\n\
411 -p, --same-permissions extract permissions information\n\
412 --no-same-permissions do not extract permissions information\n\
413 --preserve-permissions same as -p\n\
414 -s, --same-order sort names to extract to match archive\n\
415 --preserve-order same as -s\n\
416 --preserve same as both -p and -s\n"),
420 Device selection and switching:\n\
421 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
422 --force-local archive file is local even if has a colon\n\
423 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
424 -[0-7][lmh] specify drive and density\n\
425 -M, --multi-volume create/list/extract multi-volume archive\n\
426 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
427 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
428 --new-volume-script=FILE same as -F FILE\n\
429 --volno-file=FILE use/update the volume number in FILE\n"),
434 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
435 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
436 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
437 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
441 Archive format selection:\n\
442 --format=FMTNAME create archive of the given format.\n\
443 FMTNAME is one of the following:\n\
444 v7 old V7 tar format\n\
445 oldgnu GNU format as per tar <= 1.12\n\
446 ustar POSIX 1003.1-1988 (ustar) format\n\
447 posix POSIX 1003.1-2001 (pax) format\n\
449 --old-archive, --portability same as --format=v7\n\
450 --posix same as --format=posix\n\
451 -V, --label=NAME create archive with volume name NAME\n\
452 PATTERN at list/extract time, a globbing PATTERN\n\
453 -j, --bzip2 filter the archive through bzip2\n\
454 -z, --gzip, --ungzip filter the archive through gzip\n\
455 -Z, --compress, --uncompress filter the archive through compress\n\
456 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
460 Local file selection:\n\
461 -C, --directory=DIR change to directory DIR\n\
462 -T, --files-from=NAME get names to extract or create from file NAME\n\
463 --null -T reads null-terminated names, disable -C\n\
464 --exclude=PATTERN exclude files, given as a PATTERN\n\
465 -X, --exclude-from=FILE exclude patterns listed in FILE\n\
466 --anchored exclude patterns match file name start (default)\n\
467 --no-anchored exclude patterns match after any /\n\
468 --ignore-case exclusion ignores case\n\
469 --no-ignore-case exclusion is case sensitive (default)\n\
470 --wildcards exclude patterns use wildcards (default)\n\
471 --no-wildcards exclude patterns are plain strings\n\
472 --wildcards-match-slash exclude pattern wildcards match '/' (default)\n\
473 --no-wildcards-match-slash exclude pattern wildcards do not match '/'\n\
474 -P, --absolute-names don't strip leading `/'s from file names\n\
475 -h, --dereference dump instead the files symlinks point to\n\
476 --no-recursion avoid descending automatically in directories\n\
477 -l, --one-file-system stay in local file system when creating archive\n\
478 -K, --starting-file=NAME begin at file NAME in the archive\n\
479 --strip-path=NUM strip NUM leading components from file names\n\
480 before extraction\n"),
484 -N, --newer=DATE-OR-FILE only store files newer than DATE-OR-FILE\n\
485 --newer-mtime=DATE compare date and time when data changed only\n\
486 --after-date=DATE same as -N\n"),
490 --backup[=CONTROL] backup before removal, choose version control\n\
491 --suffix=SUFFIX backup before removal, override usual suffix\n"),
495 Informative output:\n\
496 --help print this help, then exit\n\
497 --version print tar program version number, then exit\n\
498 -v, --verbose verbosely list files processed\n\
499 --checkpoint print directory names while reading the archive\n\
500 --check-links print a message if not all links are dumped\n\
501 --totals print total bytes written while creating archive\n\
502 --index-file=FILE send verbose output to FILE\n\
503 -R, --block-number show block number within archive with each message\n\
504 -w, --interactive ask for confirmation for every action\n\
505 --confirmation same as -w\n"),
509 Compatibility options:\n\
510 -o when creating, same as --old-archive\n\
511 when extracting, same as --no-same-owner\n"),
516 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
517 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
519 t, numbered make numbered backups\n\
520 nil, existing numbered if numbered backups exist, simple otherwise\n\
521 never, simple always make simple backups\n"),
525 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; DATE may be a textual date\n\
526 or a file name starting with `/' or `.', in which case the file's date is used.\n\
527 *This* `tar' defaults to `--format=%s -f%s -b%d'.\n"),
528 archive_format_string (DEFAULT_ARCHIVE_FORMAT
),
529 DEFAULT_ARCHIVE
, DEFAULT_BLOCKING
);
530 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
535 /* Parse the options for tar. */
537 /* Available option letters are DEHIJQY and aenqy. Some are reserved:
539 e exit immediately with a nonzero exit status if unexpected errors occur
540 E use extended headers (draft POSIX headers, that is)
541 I same as T (for compatibility with Solaris tar)
542 n the archive is quickly seekable, so don't worry about random seeks
543 q stop after extracting the first occurrence of the named file
544 y per-file gzip compression
545 Y per-block gzip compression */
547 #define OPTION_STRING \
548 "-01234567ABC:F:GIK:L:MN:OPRST:UV:WX:Zb:cdf:g:hijklmoprstuvwxyz"
551 set_subcommand_option (enum subcommand subcommand
)
553 if (subcommand_option
!= UNKNOWN_SUBCOMMAND
554 && subcommand_option
!= subcommand
)
556 _("You may not specify more than one `-Acdtrux' option")));
558 subcommand_option
= subcommand
;
562 set_use_compress_program_option (const char *string
)
564 if (use_compress_program_option
&& strcmp (use_compress_program_option
, string
) != 0)
565 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
567 use_compress_program_option
= string
;
571 decode_options (int argc
, char **argv
)
573 int optchar
; /* option letter */
574 int input_files
; /* number of input files */
575 char const *textual_date_option
= 0;
576 char const *backup_suffix_string
;
577 char const *version_control_string
= 0;
578 int exclude_options
= EXCLUDE_WILDCARDS
;
581 /* Set some default option values. */
583 subcommand_option
= UNKNOWN_SUBCOMMAND
;
584 archive_format
= DEFAULT_FORMAT
;
585 blocking_factor
= DEFAULT_BLOCKING
;
586 record_size
= DEFAULT_BLOCKING
* BLOCKSIZE
;
587 excluded
= new_exclude ();
588 newer_mtime_option
= TYPE_MINIMUM (time_t);
589 recursion_option
= FNM_LEADING_DIR
;
594 backup_suffix_string
= getenv ("SIMPLE_BACKUP_SUFFIX");
596 /* Convert old-style tar call by exploding option element and rearranging
597 options accordingly. */
599 if (argc
> 1 && argv
[1][0] != '-')
601 int new_argc
; /* argc value for rearranged arguments */
602 char **new_argv
; /* argv value for rearranged arguments */
603 char *const *in
; /* cursor into original argv */
604 char **out
; /* cursor into rearranged argv */
605 const char *letter
; /* cursor into old option letters */
606 char buffer
[3]; /* constructed option buffer */
607 const char *cursor
; /* cursor in OPTION_STRING */
609 /* Initialize a constructed option. */
614 /* Allocate a new argument array, and copy program name in it. */
616 new_argc
= argc
- 1 + strlen (argv
[1]);
617 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (char *));
622 /* Copy each old letter option as a separate option, and have the
623 corresponding argument moved next to it. */
625 for (letter
= *in
++; *letter
; letter
++)
628 *out
++ = xstrdup (buffer
);
629 cursor
= strchr (OPTION_STRING
, *letter
);
630 if (cursor
&& cursor
[1] == ':')
632 if (in
< argv
+ argc
)
635 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
640 /* Copy all remaining options. */
642 while (in
< argv
+ argc
)
646 /* Replace the old option list by the new one. */
652 /* Parse all options and non-options as they appear. */
656 prepend_default_options (getenv ("TAR_OPTIONS"), &argc
, &argv
);
658 while (optchar
= getopt_long (argc
, argv
, OPTION_STRING
, long_options
, 0),
663 usage (TAREXIT_FAILURE
);
669 /* File name or non-parsed option, because of RETURN_IN_ORDER
670 ordering triggered by the leading dash in OPTION_STRING. */
677 set_subcommand_option (CAT_SUBCOMMAND
);
683 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
684 && u
== (blocking_factor
= u
)
685 && 0 < blocking_factor
686 && u
== (record_size
= u
* BLOCKSIZE
) / BLOCKSIZE
))
687 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
688 _("Invalid blocking factor")));
693 /* Try to reblock input records. For reading 4.2BSD pipes. */
695 /* It would surely make sense to exchange -B and -R, but it seems
696 that -B has been used for a long while in Sun tar ans most
697 BSD-derived systems. This is a consequence of the block/record
698 terminology confusion. */
700 read_full_records_option
= true;
704 set_subcommand_option (CREATE_SUBCOMMAND
);
713 set_subcommand_option (DIFF_SUBCOMMAND
);
717 if (archive_names
== allocated_archive_names
)
719 allocated_archive_names
*= 2;
721 xrealloc (archive_name_array
,
722 sizeof (const char *) * allocated_archive_names
);
724 archive_name_array
[archive_names
++] = optarg
;
728 /* Since -F is only useful with -M, make it implied. Run this
729 script at the end of each tape. */
731 info_script_option
= optarg
;
732 multi_volume_option
= true;
736 listed_incremental_option
= optarg
;
737 after_date_option
= true;
741 /* We are making an incremental dump (FIXME: are we?); save
742 directories at the beginning of the archive, and include in each
743 directory its contents. */
745 incremental_option
= true;
749 /* Follow symbolic links. */
750 dereference_option
= true;
754 /* Ignore zero blocks (eofs). This can't be the default,
755 because Unix tar writes two blocks of zeros, then pads out
756 the record with garbage. */
758 ignore_zeros_option
= true;
763 _("Warning: the -I option is not supported;"
764 " perhaps you meant -j or -T?")));
768 set_use_compress_program_option ("bzip2");
772 /* Don't replace existing files. */
773 old_files_option
= KEEP_OLD_FILES
;
777 starting_file_option
= true;
782 /* When dumping directories, don't dump files/subdirectories
783 that are on other filesystems. */
785 one_file_system_option
= true;
791 if (xstrtoumax (optarg
, 0, 10, &u
, "") != LONGINT_OK
)
792 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
793 _("Invalid tape length")));
794 tape_length_option
= 1024 * (tarlong
) u
;
795 multi_volume_option
= true;
804 /* Make multivolume archive: when we can't write any more into
805 the archive, re-open it, and continue writing. */
807 multi_volume_option
= true;
812 after_date_option
= true;
815 case NEWER_MTIME_OPTION
:
816 if (newer_mtime_option
!= TYPE_MINIMUM (time_t))
817 USAGE_ERROR ((0, 0, _("More than one threshold date")));
819 if (FILESYSTEM_PREFIX_LEN (optarg
) != 0
824 if (deref_stat (dereference_option
, optarg
, &st
) != 0)
827 USAGE_ERROR ((0, 0, _("Date file not found")));
829 newer_mtime_option
= st
.st_mtime
;
833 newer_mtime_option
= get_date (optarg
, 0);
834 if (newer_mtime_option
== (time_t) -1)
835 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
836 tartime (newer_mtime_option
), quote (optarg
)));
838 textual_date_option
= optarg
;
842 #endif /* not MSDOS */
849 to_stdout_option
= true;
853 same_permissions_option
= true;
857 absolute_names_option
= true;
861 set_subcommand_option (APPEND_SUBCOMMAND
);
865 /* Print block numbers for debugging bad tar archives. */
867 /* It would surely make sense to exchange -B and -R, but it seems
868 that -B has been used for a long while in Sun tar ans most
869 BSD-derived systems. This is a consequence of the block/record
870 terminology confusion. */
872 block_number_option
= true;
876 /* Names to extr are sorted. */
878 same_order_option
= true;
882 sparse_option
= true;
886 set_subcommand_option (LIST_SUBCOMMAND
);
891 files_from_option
= optarg
;
895 set_subcommand_option (UPDATE_SUBCOMMAND
);
899 old_files_option
= UNLINK_FIRST_OLD_FILES
;
907 volume_label_option
= optarg
;
911 interactive_option
= true;
915 verify_option
= true;
919 set_subcommand_option (EXTRACT_SUBCOMMAND
);
923 if (add_exclude_file (add_exclude
, excluded
, optarg
,
924 exclude_options
| recursion_option
, '\n')
928 FATAL_ERROR ((0, e
, "%s", quotearg_colon (optarg
)));
934 _("Warning: the -y option is not supported;"
935 " perhaps you meant -j?")));
939 set_use_compress_program_option ("gzip");
943 set_use_compress_program_option ("compress");
946 case ANCHORED_OPTION
:
947 exclude_options
|= EXCLUDE_ANCHORED
;
950 case ATIME_PRESERVE_OPTION
:
951 atime_preserve_option
= true;
954 case CHECKPOINT_OPTION
:
955 checkpoint_option
= true;
959 backup_option
= true;
961 version_control_string
= optarg
;
965 set_subcommand_option (DELETE_SUBCOMMAND
);
969 add_exclude (excluded
, optarg
, exclude_options
| recursion_option
);
972 case FORCE_LOCAL_OPTION
:
973 force_local_option
= true;
977 set_archive_format (optarg
);
980 case INDEX_FILE_OPTION
:
981 index_file_name
= optarg
;
984 case IGNORE_CASE_OPTION
:
985 exclude_options
|= FNM_CASEFOLD
;
988 case IGNORE_FAILED_READ_OPTION
:
989 ignore_failed_read_option
= true;
993 if (! (strlen (optarg
) < GNAME_FIELD_SIZE
994 && gname_to_gid (optarg
, &group_option
)))
997 if (xstrtoumax (optarg
, 0, 10, &g
, "") == LONGINT_OK
1001 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1002 _("%s: Invalid group")));
1008 = mode_compile (optarg
,
1009 MODE_MASK_EQUALS
| MODE_MASK_PLUS
| MODE_MASK_MINUS
);
1010 if (mode_option
== MODE_INVALID
)
1011 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
1012 if (mode_option
== MODE_MEMORY_EXHAUSTED
)
1016 case NO_ANCHORED_OPTION
:
1017 exclude_options
&= ~ EXCLUDE_ANCHORED
;
1020 case NO_IGNORE_CASE_OPTION
:
1021 exclude_options
&= ~ FNM_CASEFOLD
;
1024 case NO_OVERWRITE_DIR_OPTION
:
1025 old_files_option
= NO_OVERWRITE_DIR_OLD_FILES
;
1028 case NO_WILDCARDS_OPTION
:
1029 exclude_options
&= ~ EXCLUDE_WILDCARDS
;
1032 case NO_WILDCARDS_MATCH_SLASH_OPTION
:
1033 exclude_options
|= FNM_FILE_NAME
;
1037 filename_terminator
= '\0';
1040 case NUMERIC_OWNER_OPTION
:
1041 numeric_owner_option
= true;
1044 case OCCURRENCE_OPTION
:
1046 occurrence_option
= 1;
1050 if (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
)
1051 occurrence_option
= u
;
1053 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1054 _("Invalid number")));
1058 case OVERWRITE_OPTION
:
1059 old_files_option
= OVERWRITE_OLD_FILES
;
1063 if (! (strlen (optarg
) < UNAME_FIELD_SIZE
1064 && uname_to_uid (optarg
, &owner_option
)))
1067 if (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
1071 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1072 _("Invalid owner")));
1077 set_archive_format ("posix");
1080 case PRESERVE_OPTION
:
1081 same_permissions_option
= true;
1082 same_order_option
= true;
1085 case RECORD_SIZE_OPTION
:
1088 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
1089 && u
== (size_t) u
))
1090 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1091 _("Invalid record size")));
1093 if (record_size
% BLOCKSIZE
!= 0)
1094 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
1096 blocking_factor
= record_size
/ BLOCKSIZE
;
1100 case RECURSIVE_UNLINK_OPTION
:
1101 recursive_unlink_option
= true;
1104 case REMOVE_FILES_OPTION
:
1105 remove_files_option
= true;
1108 case RSH_COMMAND_OPTION
:
1109 rsh_command_option
= optarg
;
1112 case STRIP_PATH_OPTION
:
1115 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
1116 && u
== (size_t) u
))
1117 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1118 _("Invalid number of elements")));
1119 strip_path_elements
= u
;
1124 backup_option
= true;
1125 backup_suffix_string
= optarg
;
1129 totals_option
= true;
1132 case USE_COMPRESS_PROGRAM_OPTION
:
1133 set_use_compress_program_option (optarg
);
1136 case VOLNO_FILE_OPTION
:
1137 volno_file_option
= optarg
;
1140 case WILDCARDS_OPTION
:
1141 exclude_options
|= EXCLUDE_WILDCARDS
;
1144 case WILDCARDS_MATCH_SLASH_OPTION
:
1145 exclude_options
&= ~ FNM_FILE_NAME
;
1157 #ifdef DEVICE_PREFIX
1159 int device
= optchar
- '0';
1161 static char buf
[sizeof DEVICE_PREFIX
+ 10];
1164 density
= getopt_long (argc
, argv
, "lmh", 0, 0);
1165 strcpy (buf
, DEVICE_PREFIX
);
1166 cursor
= buf
+ strlen (buf
);
1168 #ifdef DENSITY_LETTER
1170 sprintf (cursor
, "%d%c", device
, density
);
1172 #else /* not DENSITY_LETTER */
1199 usage (TAREXIT_FAILURE
);
1201 sprintf (cursor
, "%d", device
);
1203 #endif /* not DENSITY_LETTER */
1205 if (archive_names
== allocated_archive_names
)
1207 allocated_archive_names
*= 2;
1208 archive_name_array
=
1209 xrealloc (archive_name_array
,
1210 sizeof (const char *) * allocated_archive_names
);
1212 archive_name_array
[archive_names
++] = strdup (buf
);
1216 #else /* not DEVICE_PREFIX */
1219 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1221 #endif /* not DEVICE_PREFIX */
1224 /* Special handling for 'o' option:
1226 GNU tar used to say "output old format".
1227 UNIX98 tar says don't chown files after extracting (we use
1228 "--no-same-owner" for this).
1230 The old GNU tar semantics is retained when used with --create
1231 option, otherwise UNIX98 semantics is assumed */
1235 if (subcommand_option
== CREATE_SUBCOMMAND
)
1237 /* GNU Tar <= 1.13 compatibility */
1238 set_archive_format ("v7");
1242 /* UNIX98 compatibility */
1243 same_owner_option
= 1;
1247 /* Handle operands after any "--" argument. */
1248 for (; optind
< argc
; optind
++)
1250 name_add (argv
[optind
]);
1254 /* Process trivial options. */
1258 printf ("tar (%s) %s\n%s\n", PACKAGE_NAME
, PACKAGE_VERSION
,
1259 "Copyright (C) 2003 Free Software Foundation, Inc.");
1261 This program comes with NO WARRANTY, to the extent permitted by law.\n\
1262 You may redistribute it under the terms of the GNU General Public License;\n\
1263 see the file named COPYING for details."));
1265 puts (_("Written by John Gilmore and Jay Fenlason."));
1267 exit (TAREXIT_SUCCESS
);
1271 usage (TAREXIT_SUCCESS
);
1273 /* Derive option values and check option consistency. */
1275 if (archive_format
== DEFAULT_FORMAT
)
1276 archive_format
= DEFAULT_ARCHIVE_FORMAT
;
1278 if (archive_format
== GNU_FORMAT
&& getenv ("POSIXLY_CORRECT"))
1279 archive_format
= POSIX_FORMAT
; /*FIXME?*/
1281 if (volume_label_option
&& subcommand_option
== CREATE_SUBCOMMAND
)
1282 assert_format (FORMAT_MASK (OLDGNU_FORMAT
)
1283 | FORMAT_MASK (GNU_FORMAT
));
1285 if (incremental_option
1286 || multi_volume_option
1288 assert_format (FORMAT_MASK (OLDGNU_FORMAT
)
1289 | FORMAT_MASK (GNU_FORMAT
)
1290 | FORMAT_MASK (POSIX_FORMAT
));
1292 if (occurrence_option
)
1294 if (!input_files
&& !files_from_option
)
1296 _("--occurrence is meaningless without file list")));
1297 if (subcommand_option
!= DELETE_SUBCOMMAND
1298 && subcommand_option
!= DIFF_SUBCOMMAND
1299 && subcommand_option
!= EXTRACT_SUBCOMMAND
1300 && subcommand_option
!= LIST_SUBCOMMAND
)
1302 _("--occurrence cannot be used in the requested operation mode")));
1305 if (archive_names
== 0)
1307 /* If no archive file name given, try TAPE from the environment, or
1308 else, DEFAULT_ARCHIVE from the configuration process. */
1311 archive_name_array
[0] = getenv ("TAPE");
1312 if (! archive_name_array
[0])
1313 archive_name_array
[0] = DEFAULT_ARCHIVE
;
1316 /* Allow multiple archives only with `-M'. */
1318 if (archive_names
> 1 && !multi_volume_option
)
1320 _("Multiple archive files requires `-M' option")));
1322 if (listed_incremental_option
1323 && newer_mtime_option
!= TYPE_MINIMUM (time_t))
1325 _("Cannot combine --listed-incremental with --newer")));
1327 if (volume_label_option
)
1329 size_t volume_label_max_len
=
1330 (sizeof current_header
->header
.name
1331 - 1 /* for trailing '\0' */
1332 - (multi_volume_option
1333 ? (sizeof " Volume "
1334 - 1 /* for null at end of " Volume " */
1335 + INT_STRLEN_BOUND (int) /* for volume number */
1336 - 1 /* for sign, as 0 <= volno */)
1338 if (volume_label_max_len
< strlen (volume_label_option
))
1340 ngettext ("%s: Volume label is too long (limit is %lu byte)",
1341 "%s: Volume label is too long (limit is %lu bytes)",
1342 volume_label_max_len
),
1343 quotearg_colon (volume_label_option
),
1344 (unsigned long) volume_label_max_len
));
1347 /* If ready to unlink hierarchies, so we are for simpler files. */
1348 if (recursive_unlink_option
)
1349 old_files_option
= UNLINK_FIRST_OLD_FILES
;
1351 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1352 explicit or implied, is used correctly. */
1354 switch (subcommand_option
)
1356 case CREATE_SUBCOMMAND
:
1357 if (input_files
== 0 && !files_from_option
)
1359 _("Cowardly refusing to create an empty archive")));
1362 case EXTRACT_SUBCOMMAND
:
1363 case LIST_SUBCOMMAND
:
1364 case DIFF_SUBCOMMAND
:
1365 for (archive_name_cursor
= archive_name_array
;
1366 archive_name_cursor
< archive_name_array
+ archive_names
;
1367 archive_name_cursor
++)
1368 if (!strcmp (*archive_name_cursor
, "-"))
1369 request_stdin ("-f");
1372 case CAT_SUBCOMMAND
:
1373 case UPDATE_SUBCOMMAND
:
1374 case APPEND_SUBCOMMAND
:
1375 for (archive_name_cursor
= archive_name_array
;
1376 archive_name_cursor
< archive_name_array
+ archive_names
;
1377 archive_name_cursor
++)
1378 if (!strcmp (*archive_name_cursor
, "-"))
1380 _("Options `-Aru' are incompatible with `-f -'")));
1386 archive_name_cursor
= archive_name_array
;
1388 /* Prepare for generating backup names. */
1390 if (backup_suffix_string
)
1391 simple_backup_suffix
= xstrdup (backup_suffix_string
);
1394 backup_type
= xget_version ("--backup", version_control_string
);
1396 if (verbose_option
&& textual_date_option
)
1398 char const *treated_as
= tartime (newer_mtime_option
);
1399 if (strcmp (textual_date_option
, treated_as
) != 0)
1400 WARN ((0, 0, _("Treating date `%s' as %s"),
1401 textual_date_option
, treated_as
));
1408 /* Main routine for tar. */
1410 main (int argc
, char **argv
)
1412 #if HAVE_CLOCK_GETTIME
1413 if (clock_gettime (CLOCK_REALTIME
, &start_timespec
) != 0)
1415 start_time
= time (0);
1416 program_name
= argv
[0];
1417 setlocale (LC_ALL
, "");
1418 bindtextdomain (PACKAGE
, LOCALEDIR
);
1419 textdomain (PACKAGE
);
1421 exit_status
= TAREXIT_SUCCESS
;
1422 filename_terminator
= '\n';
1423 set_quoting_style (0, escape_quoting_style
);
1425 /* Pre-allocate a few structures. */
1427 allocated_archive_names
= 10;
1428 archive_name_array
=
1429 xmalloc (sizeof (const char *) * allocated_archive_names
);
1433 /* System V fork+wait does not work if SIGCHLD is ignored. */
1434 signal (SIGCHLD
, SIG_DFL
);
1439 /* Decode options. */
1441 decode_options (argc
, argv
);
1442 name_init (argc
, argv
);
1444 /* Main command execution. */
1446 if (volno_file_option
)
1447 init_volume_number ();
1449 switch (subcommand_option
)
1451 case UNKNOWN_SUBCOMMAND
:
1453 _("You must specify one of the `-Acdtrux' options")));
1455 case CAT_SUBCOMMAND
:
1456 case UPDATE_SUBCOMMAND
:
1457 case APPEND_SUBCOMMAND
:
1461 case DELETE_SUBCOMMAND
:
1462 delete_archive_members ();
1465 case CREATE_SUBCOMMAND
:
1470 print_total_written ();
1473 case EXTRACT_SUBCOMMAND
:
1475 read_and (extract_archive
);
1477 /* FIXME: should extract_finish () even if an ordinary signal is
1483 case LIST_SUBCOMMAND
:
1484 read_and (list_archive
);
1487 case DIFF_SUBCOMMAND
:
1489 read_and (diff_archive
);
1493 if (check_links_option
)
1496 if (volno_file_option
)
1497 closeout_volume_number ();
1499 /* Dispose of allocated memory, and return. */
1501 free (archive_name_array
);
1504 if (stdlis
!= stderr
&& (ferror (stdlis
) || fclose (stdlis
) != 0))
1505 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1506 if (exit_status
== TAREXIT_FAILURE
)
1507 error (0, 0, _("Error exit delayed from previous errors"));
1508 if (ferror (stderr
) || fclose (stderr
) != 0)
1509 exit_status
= TAREXIT_FAILURE
;
1514 tar_stat_init (struct tar_stat_info
*st
)
1516 memset (st
, 0, sizeof (*st
));
1520 tar_stat_destroy (struct tar_stat_info
*st
)
1522 free (st
->orig_file_name
);
1523 free (st
->file_name
);
1524 free (st
->link_name
);
1527 free (st
->sparse_map
);
1528 memset (st
, 0, sizeof (*st
));