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 {"check-links", no_argument
, &check_links_option
, 1},
199 {"compare", no_argument
, 0, 'd'},
200 {"compress", no_argument
, 0, 'Z'},
201 {"concatenate", no_argument
, 0, 'A'},
202 {"confirmation", no_argument
, 0, 'w'},
203 /* FIXME: --selective as a synonym for --confirmation? */
204 {"create", no_argument
, 0, 'c'},
205 {"delete", no_argument
, 0, DELETE_OPTION
},
206 {"dereference", no_argument
, 0, 'h'},
207 {"diff", no_argument
, 0, 'd'},
208 {"directory", required_argument
, 0, 'C'},
209 {"exclude", required_argument
, 0, EXCLUDE_OPTION
},
210 {"exclude-from", required_argument
, 0, 'X'},
211 {"extract", no_argument
, 0, 'x'},
212 {"file", required_argument
, 0, 'f'},
213 {"files-from", required_argument
, 0, 'T'},
214 {"force-local", no_argument
, 0, FORCE_LOCAL_OPTION
},
215 {"get", no_argument
, 0, 'x'},
216 {"group", required_argument
, 0, GROUP_OPTION
},
217 {"gunzip", no_argument
, 0, 'z'},
218 {"gzip", no_argument
, 0, 'z'},
219 {"help", no_argument
, &show_help
, 1},
220 {"ignore-case", no_argument
, 0, IGNORE_CASE_OPTION
},
221 {"ignore-failed-read", no_argument
, 0, IGNORE_FAILED_READ_OPTION
},
222 {"ignore-zeros", no_argument
, 0, 'i'},
223 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
224 {"incremental", no_argument
, 0, 'G'},
225 {"index-file", required_argument
, 0, INDEX_FILE_OPTION
},
226 {"info-script", required_argument
, 0, 'F'},
227 {"interactive", no_argument
, 0, 'w'},
228 {"keep-old-files", no_argument
, 0, 'k'},
229 {"label", required_argument
, 0, 'V'},
230 {"list", no_argument
, 0, 't'},
231 {"listed-incremental", required_argument
, 0, 'g'},
232 {"mode", required_argument
, 0, MODE_OPTION
},
233 {"modification-time", no_argument
, 0, OBSOLETE_TOUCH
},
234 {"multi-volume", no_argument
, 0, 'M'},
235 {"new-volume-script", required_argument
, 0, 'F'},
236 {"newer", required_argument
, 0, 'N'},
237 {"newer-mtime", required_argument
, 0, NEWER_MTIME_OPTION
},
238 {"null", no_argument
, 0, NULL_OPTION
},
239 {"no-anchored", no_argument
, 0, NO_ANCHORED_OPTION
},
240 {"no-ignore-case", no_argument
, 0, NO_IGNORE_CASE_OPTION
},
241 {"no-overwrite-dir", no_argument
, 0, NO_OVERWRITE_DIR_OPTION
},
242 {"no-wildcards", no_argument
, 0, NO_WILDCARDS_OPTION
},
243 {"no-wildcards-match-slash", no_argument
, 0, NO_WILDCARDS_MATCH_SLASH_OPTION
},
244 {"no-recursion", no_argument
, &recursion_option
, 0},
245 {"no-same-owner", no_argument
, &same_owner_option
, -1},
246 {"no-same-permissions", no_argument
, &same_permissions_option
, -1},
247 {"numeric-owner", no_argument
, 0, NUMERIC_OWNER_OPTION
},
248 {"old-archive", no_argument
, 0, 'o'},
249 {"one-file-system", no_argument
, 0, 'l'},
250 {"overwrite", no_argument
, 0, OVERWRITE_OPTION
},
251 {"owner", required_argument
, 0, OWNER_OPTION
},
252 {"portability", no_argument
, 0, 'o'},
253 {"posix", no_argument
, 0, POSIX_OPTION
},
254 {"preserve", no_argument
, 0, PRESERVE_OPTION
},
255 {"preserve-order", no_argument
, 0, 's'},
256 {"preserve-permissions", no_argument
, 0, 'p'},
257 {"recursion", no_argument
, &recursion_option
, FNM_LEADING_DIR
},
258 {"recursive-unlink", no_argument
, 0, RECURSIVE_UNLINK_OPTION
},
259 {"read-full-blocks", no_argument
, 0, OBSOLETE_READ_FULL_RECORDS
},
260 {"read-full-records", no_argument
, 0, 'B'},
261 /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
262 {"record-number", no_argument
, 0, OBSOLETE_BLOCK_NUMBER
},
263 {"record-size", required_argument
, 0, RECORD_SIZE_OPTION
},
264 {"remove-files", no_argument
, 0, REMOVE_FILES_OPTION
},
265 {"rsh-command", required_argument
, 0, RSH_COMMAND_OPTION
},
266 {"same-order", no_argument
, 0, 's'},
267 {"same-owner", no_argument
, &same_owner_option
, 1},
268 {"same-permissions", no_argument
, 0, 'p'},
269 {"show-omitted-dirs", no_argument
, 0, SHOW_OMITTED_DIRS_OPTION
},
270 {"sparse", no_argument
, 0, 'S'},
271 {"starting-file", required_argument
, 0, 'K'},
272 {"suffix", required_argument
, 0, SUFFIX_OPTION
},
273 {"tape-length", required_argument
, 0, 'L'},
274 {"to-stdout", no_argument
, 0, 'O'},
275 {"totals", no_argument
, 0, TOTALS_OPTION
},
276 {"touch", no_argument
, 0, 'm'},
277 {"uncompress", no_argument
, 0, 'Z'},
278 {"ungzip", no_argument
, 0, 'z'},
279 {"unlink-first", no_argument
, 0, 'U'},
280 {"update", no_argument
, 0, 'u'},
281 {"use-compress-program", required_argument
, 0, USE_COMPRESS_PROGRAM_OPTION
},
282 {"verbose", no_argument
, 0, 'v'},
283 {"verify", no_argument
, 0, 'W'},
284 {"version", no_argument
, &show_version
, 1},
285 {"version-control", required_argument
, 0, OBSOLETE_VERSION_CONTROL
},
286 {"volno-file", required_argument
, 0, VOLNO_FILE_OPTION
},
287 {"wildcards", no_argument
, 0, WILDCARDS_OPTION
},
288 {"wildcards-match-slash", no_argument
, 0, WILDCARDS_MATCH_SLASH_OPTION
},
293 /* Print a usage message and exit with STATUS. */
297 if (status
!= TAREXIT_SUCCESS
)
298 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
303 GNU `tar' saves many files together into a single tape or disk archive, and\n\
304 can restore individual files from the archive.\n"),
306 printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
309 %s -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
310 %s -tvf archive.tar # List all files in archive.tar verbosely.\n\
311 %s -xf archive.tar # Extract all files from archive.tar.\n"),
312 program_name
, program_name
, program_name
, program_name
);
315 If a long option shows an argument as mandatory, then it is mandatory\n\
316 for the equivalent short option also. Similarly for optional arguments.\n"),
320 Main operation mode:\n\
321 -t, --list list the contents of an archive\n\
322 -x, --extract, --get extract files from an archive\n\
323 -c, --create create a new archive\n\
324 -d, --diff, --compare find differences between archive and file system\n\
325 -r, --append append files to the end of an archive\n\
326 -u, --update only append files newer than copy in archive\n\
327 -A, --catenate append tar files to an archive\n\
328 --concatenate same as -A\n\
329 --delete delete from the archive (not on mag tapes!)\n"),
333 Operation modifiers:\n\
334 -W, --verify attempt to verify the archive after writing it\n\
335 --remove-files remove files after adding them to the archive\n\
336 -k, --keep-old-files don't replace existing files when extracting\n\
337 --overwrite overwrite existing files when extracting\n\
338 --no-overwrite-dir preserve metadata of existing directories\n\
339 -U, --unlink-first remove each file prior to extracting over it\n\
340 --recursive-unlink empty hierarchies prior to extracting directory\n\
341 -S, --sparse handle sparse files efficiently\n\
342 -O, --to-stdout extract files to standard output\n\
343 -G, --incremental handle old GNU-format incremental backup\n\
344 -g, --listed-incremental=FILE\n\
345 handle new GNU-format incremental backup\n\
346 --ignore-failed-read do not exit with nonzero on unreadable files\n"),
350 Handling of file attributes:\n\
351 --owner=NAME force NAME as owner for added files\n\
352 --group=NAME force NAME as group for added files\n\
353 --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
354 --atime-preserve don't change access times on dumped files\n\
355 -m, --modification-time don't extract file modified time\n\
356 --same-owner try extracting files with the same ownership\n\
357 --no-same-owner extract files as yourself\n\
358 --numeric-owner always use numbers for user/group names\n\
359 -p, --same-permissions extract permissions information\n\
360 --no-same-permissions do not extract permissions information\n\
361 --preserve-permissions same as -p\n\
362 -s, --same-order sort names to extract to match archive\n\
363 --preserve-order same as -s\n\
364 --preserve same as both -p and -s\n"),
368 Device selection and switching:\n\
369 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
370 --force-local archive file is local even if has a colon\n\
371 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
372 -[0-7][lmh] specify drive and density\n\
373 -M, --multi-volume create/list/extract multi-volume archive\n\
374 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
375 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
376 --new-volume-script=FILE same as -F FILE\n\
377 --volno-file=FILE use/update the volume number in FILE\n"),
382 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
383 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
384 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
385 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
389 Archive format selection:\n\
390 -V, --label=NAME create archive with volume name NAME\n\
391 PATTERN at list/extract time, a globbing PATTERN\n\
392 --old-archive, --portability write a V7 format archive\n\
393 --posix write a POSIX format archive\n\
394 -j, --bzip2 filter the archive through bzip2\n\
395 -z, --gzip, --ungzip filter the archive through gzip\n\
396 -Z, --compress, --uncompress filter the archive through compress\n\
397 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
401 Local file selection:\n\
402 -C, --directory=DIR change to directory DIR\n\
403 -T, --files-from=NAME get names to extract or create from file NAME\n\
404 --null -T reads null-terminated names, disable -C\n\
405 --exclude=PATTERN exclude files, given as a PATTERN\n\
406 -X, --exclude-from=FILE exclude patterns listed in FILE\n\
407 --anchored exclude patterns match file name start (default)\n\
408 --no-anchored exclude patterns match after any /\n\
409 --ignore-case exclusion ignores case\n\
410 --no-ignore-case exclusion is case sensitive (default)\n\
411 --wildcards exclude patterns use wildcards (default)\n\
412 --no-wildcards exclude patterns are plain strings\n\
413 --wildcards-match-slash exclude pattern wildcards match '/' (default)\n\
414 --no-wildcards-match-slash exclude pattern wildcards do not match '/'\n\
415 -P, --absolute-names don't strip leading `/'s from file names\n\
416 -h, --dereference dump instead the files symlinks point to\n\
417 --no-recursion avoid descending automatically in directories\n\
418 -l, --one-file-system stay in local file system when creating archive\n\
419 -K, --starting-file=NAME begin at file NAME in the archive\n"),
423 -N, --newer=DATE only store files newer than DATE\n\
424 --newer-mtime=DATE compare date and time when data changed only\n\
425 --after-date=DATE same as -N\n"),
429 --backup[=CONTROL] backup before removal, choose version control\n\
430 --suffix=SUFFIX backup before removal, override usual suffix\n"),
434 Informative output:\n\
435 --help print this help, then exit\n\
436 --version print tar program version number, then exit\n\
437 -v, --verbose verbosely list files processed\n\
438 --checkpoint print directory names while reading the archive\n\
439 --check-links print a message if not all links are dumped\n\
440 --totals print total bytes written while creating archive\n\
441 --index-file=FILE send verbose output to FILE\n\
442 -R, --block-number show block number within archive with each message\n\
443 -w, --interactive ask for confirmation for every action\n\
444 --confirmation same as -w\n"),
448 Compatibility options:\n\
449 -o when creating, same as --old-archive\n\
450 when extracting, same as --no-same-owner\n"),
455 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
456 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
458 t, numbered make numbered backups\n\
459 nil, existing numbered if numbered backups exist, simple otherwise\n\
460 never, simple always make simple backups\n"),
464 GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
465 is set in the environment, GNU extensions are disallowed with `--posix'.\n\
466 Support for POSIX is only partially implemented, don't count on it yet.\n\
467 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; DATE may be a textual date\n\
468 or a file name starting with `/' or `.', in which case the file's date is used.\n\
469 *This* `tar' defaults to `-f%s -b%d'.\n"),
470 DEFAULT_ARCHIVE
, DEFAULT_BLOCKING
);
471 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
476 /* Parse the options for tar. */
478 /* Available option letters are DEHIJQY and aenqy. Some are reserved:
480 e exit immediately with a nonzero exit status if unexpected errors occur
481 E use extended headers (draft POSIX headers, that is)
482 I same as T (for compatibility with Solaris tar)
483 n the archive is quickly seekable, so don't worry about random seeks
484 q stop after extracting the first occurrence of the named file
485 y per-file gzip compression
486 Y per-block gzip compression */
488 #define OPTION_STRING \
489 "-01234567ABC:F:GIK:L:MN:OPRST:UV:WX:Zb:cdf:g:hijklmoprstuvwxyz"
492 set_subcommand_option (enum subcommand subcommand
)
494 if (subcommand_option
!= UNKNOWN_SUBCOMMAND
495 && subcommand_option
!= subcommand
)
497 _("You may not specify more than one `-Acdtrux' option")));
499 subcommand_option
= subcommand
;
503 set_use_compress_program_option (const char *string
)
505 if (use_compress_program_option
&& strcmp (use_compress_program_option
, string
) != 0)
506 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
508 use_compress_program_option
= string
;
512 decode_options (int argc
, char **argv
)
514 int optchar
; /* option letter */
515 int input_files
; /* number of input files */
516 char const *textual_date_option
= 0;
517 char const *backup_suffix_string
;
518 char const *version_control_string
= 0;
519 int exclude_options
= EXCLUDE_WILDCARDS
;
522 /* Set some default option values. */
524 subcommand_option
= UNKNOWN_SUBCOMMAND
;
525 archive_format
= DEFAULT_FORMAT
;
526 blocking_factor
= DEFAULT_BLOCKING
;
527 record_size
= DEFAULT_BLOCKING
* BLOCKSIZE
;
528 excluded
= new_exclude ();
529 newer_mtime_option
= TYPE_MINIMUM (time_t);
530 recursion_option
= FNM_LEADING_DIR
;
535 backup_suffix_string
= getenv ("SIMPLE_BACKUP_SUFFIX");
537 /* Convert old-style tar call by exploding option element and rearranging
538 options accordingly. */
540 if (argc
> 1 && argv
[1][0] != '-')
542 int new_argc
; /* argc value for rearranged arguments */
543 char **new_argv
; /* argv value for rearranged arguments */
544 char *const *in
; /* cursor into original argv */
545 char **out
; /* cursor into rearranged argv */
546 const char *letter
; /* cursor into old option letters */
547 char buffer
[3]; /* constructed option buffer */
548 const char *cursor
; /* cursor in OPTION_STRING */
550 /* Initialize a constructed option. */
555 /* Allocate a new argument array, and copy program name in it. */
557 new_argc
= argc
- 1 + strlen (argv
[1]);
558 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (char *));
563 /* Copy each old letter option as a separate option, and have the
564 corresponding argument moved next to it. */
566 for (letter
= *in
++; *letter
; letter
++)
569 *out
++ = xstrdup (buffer
);
570 cursor
= strchr (OPTION_STRING
, *letter
);
571 if (cursor
&& cursor
[1] == ':')
573 if (in
< argv
+ argc
)
576 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
581 /* Copy all remaining options. */
583 while (in
< argv
+ argc
)
587 /* Replace the old option list by the new one. */
593 /* Parse all options and non-options as they appear. */
597 prepend_default_options (getenv ("TAR_OPTIONS"), &argc
, &argv
);
599 while (optchar
= getopt_long (argc
, argv
, OPTION_STRING
, long_options
, 0),
604 usage (TAREXIT_FAILURE
);
610 /* File name or non-parsed option, because of RETURN_IN_ORDER
611 ordering triggered by the leading dash in OPTION_STRING. */
618 set_subcommand_option (CAT_SUBCOMMAND
);
621 case OBSOLETE_BLOCK_COMPRESS
:
622 WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
625 case OBSOLETE_BLOCKING_FACTOR
:
626 WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
632 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
633 && u
== (blocking_factor
= u
)
634 && 0 < blocking_factor
635 && u
== (record_size
= u
* BLOCKSIZE
) / BLOCKSIZE
))
636 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
637 _("Invalid blocking factor")));
641 case OBSOLETE_READ_FULL_RECORDS
:
643 _("Obsolete option name replaced by --read-full-records")));
647 /* Try to reblock input records. For reading 4.2BSD pipes. */
649 /* It would surely make sense to exchange -B and -R, but it seems
650 that -B has been used for a long while in Sun tar ans most
651 BSD-derived systems. This is a consequence of the block/record
652 terminology confusion. */
654 read_full_records_option
= 1;
658 set_subcommand_option (CREATE_SUBCOMMAND
);
667 set_subcommand_option (DIFF_SUBCOMMAND
);
671 if (archive_names
== allocated_archive_names
)
673 allocated_archive_names
*= 2;
675 xrealloc (archive_name_array
,
676 sizeof (const char *) * allocated_archive_names
);
678 archive_name_array
[archive_names
++] = optarg
;
682 /* Since -F is only useful with -M, make it implied. Run this
683 script at the end of each tape. */
685 info_script_option
= optarg
;
686 multi_volume_option
= 1;
690 listed_incremental_option
= optarg
;
691 after_date_option
= 1;
695 /* We are making an incremental dump (FIXME: are we?); save
696 directories at the beginning of the archive, and include in each
697 directory its contents. */
699 incremental_option
= 1;
703 /* Follow symbolic links. */
705 dereference_option
= 1;
709 /* Ignore zero blocks (eofs). This can't be the default,
710 because Unix tar writes two blocks of zeros, then pads out
711 the record with garbage. */
713 ignore_zeros_option
= 1;
718 _("Warning: the -I option is not supported;"
719 " perhaps you meant -j or -T?")));
723 set_use_compress_program_option ("bzip2");
727 /* Don't replace existing files. */
728 old_files_option
= KEEP_OLD_FILES
;
732 starting_file_option
= 1;
737 /* When dumping directories, don't dump files/subdirectories
738 that are on other filesystems. */
740 one_file_system_option
= 1;
746 if (xstrtoumax (optarg
, 0, 10, &u
, "") != LONGINT_OK
)
747 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
748 _("Invalid tape length")));
749 tape_length_option
= 1024 * (tarlong
) u
;
750 multi_volume_option
= 1;
755 WARN ((0, 0, _("Obsolete option name replaced by --touch")));
763 /* Make multivolume archive: when we can't write any more into
764 the archive, re-open it, and continue writing. */
766 multi_volume_option
= 1;
771 after_date_option
= 1;
774 case NEWER_MTIME_OPTION
:
775 if (newer_mtime_option
!= TYPE_MINIMUM (time_t))
776 USAGE_ERROR ((0, 0, _("More than one threshold date")));
778 if (FILESYSTEM_PREFIX_LEN (optarg
) != 0
783 if (deref_stat (dereference_option
, optarg
, &st
) != 0)
786 USAGE_ERROR ((0, 0, _("Date file not found")));
788 newer_mtime_option
= st
.st_mtime
;
792 newer_mtime_option
= get_date (optarg
, 0);
793 if (newer_mtime_option
== (time_t) -1)
794 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
795 tartime (newer_mtime_option
), quote (optarg
)));
797 textual_date_option
= optarg
;
801 #endif /* not MSDOS */
808 to_stdout_option
= 1;
812 same_permissions_option
= 1;
815 case OBSOLETE_ABSOLUTE_NAMES
:
816 WARN ((0, 0, _("Obsolete option name replaced by --absolute-names")));
820 absolute_names_option
= 1;
824 set_subcommand_option (APPEND_SUBCOMMAND
);
827 case OBSOLETE_BLOCK_NUMBER
:
828 WARN ((0, 0, _("Obsolete option name replaced by --block-number")));
832 /* Print block numbers for debugging bad tar archives. */
834 /* It would surely make sense to exchange -B and -R, but it seems
835 that -B has been used for a long while in Sun tar ans most
836 BSD-derived systems. This is a consequence of the block/record
837 terminology confusion. */
839 block_number_option
= 1;
843 /* Names to extr are sorted. */
845 same_order_option
= 1;
853 set_subcommand_option (LIST_SUBCOMMAND
);
858 files_from_option
= optarg
;
862 set_subcommand_option (UPDATE_SUBCOMMAND
);
866 old_files_option
= UNLINK_FIRST_OLD_FILES
;
874 volume_label_option
= optarg
;
878 interactive_option
= 1;
886 set_subcommand_option (EXTRACT_SUBCOMMAND
);
890 if (add_exclude_file (add_exclude
, excluded
, optarg
,
891 exclude_options
| recursion_option
, '\n')
895 FATAL_ERROR ((0, e
, "%s", quotearg_colon (optarg
)));
901 _("Warning: the -y option is not supported;"
902 " perhaps you meant -j?")));
906 set_use_compress_program_option ("gzip");
910 set_use_compress_program_option ("compress");
913 case OBSOLETE_VERSION_CONTROL
:
914 WARN ((0, 0, _("Obsolete option name replaced by --backup")));
917 case ANCHORED_OPTION
:
918 exclude_options
|= EXCLUDE_ANCHORED
;
921 case ATIME_PRESERVE_OPTION
:
922 atime_preserve_option
= 1;
925 case CHECKPOINT_OPTION
:
926 checkpoint_option
= 1;
932 version_control_string
= optarg
;
936 set_subcommand_option (DELETE_SUBCOMMAND
);
940 add_exclude (excluded
, optarg
, exclude_options
| recursion_option
);
943 case FORCE_LOCAL_OPTION
:
944 force_local_option
= 1;
947 case INDEX_FILE_OPTION
:
948 index_file_name
= optarg
;
951 case IGNORE_CASE_OPTION
:
952 exclude_options
|= FNM_CASEFOLD
;
955 case IGNORE_FAILED_READ_OPTION
:
956 ignore_failed_read_option
= 1;
960 if (! (strlen (optarg
) < GNAME_FIELD_SIZE
961 && gname_to_gid (optarg
, &group_option
)))
964 if (xstrtoumax (optarg
, 0, 10, &g
, "") == LONGINT_OK
968 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
969 _("%s: Invalid group")));
975 = mode_compile (optarg
,
976 MODE_MASK_EQUALS
| MODE_MASK_PLUS
| MODE_MASK_MINUS
);
977 if (mode_option
== MODE_INVALID
)
978 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
979 if (mode_option
== MODE_MEMORY_EXHAUSTED
)
983 case NO_ANCHORED_OPTION
:
984 exclude_options
&= ~ EXCLUDE_ANCHORED
;
987 case NO_IGNORE_CASE_OPTION
:
988 exclude_options
&= ~ FNM_CASEFOLD
;
991 case NO_OVERWRITE_DIR_OPTION
:
992 old_files_option
= NO_OVERWRITE_DIR_OLD_FILES
;
995 case NO_WILDCARDS_OPTION
:
996 exclude_options
&= ~ EXCLUDE_WILDCARDS
;
999 case NO_WILDCARDS_MATCH_SLASH_OPTION
:
1000 exclude_options
|= FNM_FILE_NAME
;
1004 filename_terminator
= '\0';
1007 case NUMERIC_OWNER_OPTION
:
1008 numeric_owner_option
= 1;
1011 case OVERWRITE_OPTION
:
1012 old_files_option
= OVERWRITE_OLD_FILES
;
1016 if (! (strlen (optarg
) < UNAME_FIELD_SIZE
1017 && uname_to_uid (optarg
, &owner_option
)))
1020 if (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
1024 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1025 _("Invalid owner")));
1030 #if OLDGNU_COMPATIBILITY
1031 if (archive_format
== DEFAULT_FORMAT
)
1032 archive_format
= GNU_FORMAT
;
1033 else if (archive_format
!= GNU_FORMAT
)
1034 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
1036 if (archive_format
== DEFAULT_FORMAT
)
1037 archive_format
= POSIX_FORMAT
;
1038 else if (archive_format
!= POSIX_FORMAT
)
1039 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
1043 case PRESERVE_OPTION
:
1044 same_permissions_option
= 1;
1045 same_order_option
= 1;
1048 case RECORD_SIZE_OPTION
:
1051 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
1052 && u
== (size_t) u
))
1053 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1054 _("Invalid record size")));
1056 if (record_size
% BLOCKSIZE
!= 0)
1057 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
1059 blocking_factor
= record_size
/ BLOCKSIZE
;
1063 case RECURSIVE_UNLINK_OPTION
:
1064 recursive_unlink_option
= 1;
1067 case REMOVE_FILES_OPTION
:
1068 remove_files_option
= 1;
1071 case RSH_COMMAND_OPTION
:
1072 rsh_command_option
= optarg
;
1077 backup_suffix_string
= optarg
;
1080 case USE_COMPRESS_PROGRAM_OPTION
:
1081 set_use_compress_program_option (optarg
);
1084 case VOLNO_FILE_OPTION
:
1085 volno_file_option
= optarg
;
1088 case WILDCARDS_OPTION
:
1089 exclude_options
|= EXCLUDE_WILDCARDS
;
1092 case WILDCARDS_MATCH_SLASH_OPTION
:
1093 exclude_options
&= ~ FNM_FILE_NAME
;
1105 #ifdef DEVICE_PREFIX
1107 int device
= optchar
- '0';
1109 static char buf
[sizeof DEVICE_PREFIX
+ 10];
1112 density
= getopt_long (argc
, argv
, "lmh", 0, 0);
1113 strcpy (buf
, DEVICE_PREFIX
);
1114 cursor
= buf
+ strlen (buf
);
1116 #ifdef DENSITY_LETTER
1118 sprintf (cursor
, "%d%c", device
, density
);
1120 #else /* not DENSITY_LETTER */
1147 usage (TAREXIT_FAILURE
);
1149 sprintf (cursor
, "%d", device
);
1151 #endif /* not DENSITY_LETTER */
1153 if (archive_names
== allocated_archive_names
)
1155 allocated_archive_names
*= 2;
1156 archive_name_array
=
1157 xrealloc (archive_name_array
,
1158 sizeof (const char *) * allocated_archive_names
);
1160 archive_name_array
[archive_names
++] = buf
;
1162 /* FIXME: How comes this works for many archives when buf is
1167 #else /* not DEVICE_PREFIX */
1170 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1172 #endif /* not DEVICE_PREFIX */
1175 /* Special handling for 'o' option:
1177 GNU tar used to say "output old format".
1178 UNIX98 tar says don't chown files after extracting (we use
1179 "--no-same-owner" for this).
1181 The old GNU tar semantics is retained when used with --create
1182 option, otherwise UNIX98 semantics is assumed */
1186 if (subcommand_option
== CREATE_SUBCOMMAND
)
1188 /* GNU Tar <= 1.13 compatibility */
1189 if (archive_format
== DEFAULT_FORMAT
)
1190 archive_format
= V7_FORMAT
;
1191 else if (archive_format
!= V7_FORMAT
)
1192 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
1196 /* UNIX98 compatibility */
1197 same_owner_option
= 1;
1201 /* Handle operands after any "--" argument. */
1202 for (; optind
< argc
; optind
++)
1204 name_add (argv
[optind
]);
1208 /* Process trivial options. */
1212 printf ("tar (%s) %s\n", PACKAGE_NAME
, PACKAGE_VERSION
);
1213 printf (_("Copyright (C) %d Free Software Foundation, Inc.\n"), 2003);
1215 This program comes with NO WARRANTY, to the extent permitted by law.\n\
1216 You may redistribute it under the terms of the GNU General Public License;\n\
1217 see the file named COPYING for details."));
1219 puts (_("Written by John Gilmore and Jay Fenlason."));
1221 exit (TAREXIT_SUCCESS
);
1225 usage (TAREXIT_SUCCESS
);
1227 /* Derive option values and check option consistency. */
1229 if (archive_format
== DEFAULT_FORMAT
)
1231 #if OLDGNU_COMPATIBILITY
1232 archive_format
= OLDGNU_FORMAT
;
1234 archive_format
= GNU_FORMAT
;
1238 if (archive_format
== GNU_FORMAT
&& getenv ("POSIXLY_CORRECT"))
1239 archive_format
= POSIX_FORMAT
;
1241 if ((volume_label_option
1242 || incremental_option
|| multi_volume_option
|| sparse_option
)
1243 && archive_format
!= OLDGNU_FORMAT
&& archive_format
!= GNU_FORMAT
)
1245 _("GNU features wanted on incompatible archive format")));
1247 if (archive_names
== 0)
1249 /* If no archive file name given, try TAPE from the environment, or
1250 else, DEFAULT_ARCHIVE from the configuration process. */
1253 archive_name_array
[0] = getenv ("TAPE");
1254 if (! archive_name_array
[0])
1255 archive_name_array
[0] = DEFAULT_ARCHIVE
;
1258 /* Allow multiple archives only with `-M'. */
1260 if (archive_names
> 1 && !multi_volume_option
)
1262 _("Multiple archive files requires `-M' option")));
1264 if (listed_incremental_option
1265 && newer_mtime_option
!= TYPE_MINIMUM (time_t))
1267 _("Cannot combine --listed-incremental with --newer")));
1269 if (volume_label_option
)
1271 size_t volume_label_max_len
=
1272 (sizeof current_header
->header
.name
1273 - 1 /* for trailing '\0' */
1274 - (multi_volume_option
1275 ? (sizeof " Volume "
1276 - 1 /* for null at end of " Volume " */
1277 + INT_STRLEN_BOUND (int) /* for volume number */
1278 - 1 /* for sign, as 0 <= volno */)
1280 if (volume_label_max_len
< strlen (volume_label_option
))
1282 _("%s: Volume label is too long (limit is %lu bytes)"),
1283 quotearg_colon (volume_label_option
),
1284 (unsigned long) volume_label_max_len
));
1287 /* If ready to unlink hierarchies, so we are for simpler files. */
1288 if (recursive_unlink_option
)
1289 old_files_option
= UNLINK_FIRST_OLD_FILES
;
1291 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1292 explicit or implied, is used correctly. */
1294 switch (subcommand_option
)
1296 case CREATE_SUBCOMMAND
:
1297 if (input_files
== 0 && !files_from_option
)
1299 _("Cowardly refusing to create an empty archive")));
1302 case EXTRACT_SUBCOMMAND
:
1303 case LIST_SUBCOMMAND
:
1304 case DIFF_SUBCOMMAND
:
1305 for (archive_name_cursor
= archive_name_array
;
1306 archive_name_cursor
< archive_name_array
+ archive_names
;
1307 archive_name_cursor
++)
1308 if (!strcmp (*archive_name_cursor
, "-"))
1309 request_stdin ("-f");
1312 case CAT_SUBCOMMAND
:
1313 case UPDATE_SUBCOMMAND
:
1314 case APPEND_SUBCOMMAND
:
1315 for (archive_name_cursor
= archive_name_array
;
1316 archive_name_cursor
< archive_name_array
+ archive_names
;
1317 archive_name_cursor
++)
1318 if (!strcmp (*archive_name_cursor
, "-"))
1320 _("Options `-Aru' are incompatible with `-f -'")));
1326 archive_name_cursor
= archive_name_array
;
1328 /* Prepare for generating backup names. */
1330 if (backup_suffix_string
)
1331 simple_backup_suffix
= xstrdup (backup_suffix_string
);
1334 backup_type
= xget_version ("--backup", version_control_string
);
1336 if (verbose_option
&& textual_date_option
)
1338 char const *treated_as
= tartime (newer_mtime_option
);
1339 if (strcmp (textual_date_option
, treated_as
) != 0)
1340 WARN ((0, 0, _("Treating date `%s' as %s"),
1341 textual_date_option
, treated_as
));
1347 /* Main routine for tar. */
1349 main (int argc
, char **argv
)
1351 #if HAVE_CLOCK_GETTIME
1352 if (clock_gettime (CLOCK_REALTIME
, &start_timespec
) != 0)
1354 start_time
= time (0);
1355 program_name
= argv
[0];
1356 setlocale (LC_ALL
, "");
1357 bindtextdomain (PACKAGE
, LOCALEDIR
);
1358 textdomain (PACKAGE
);
1360 exit_status
= TAREXIT_SUCCESS
;
1361 filename_terminator
= '\n';
1362 set_quoting_style (0, escape_quoting_style
);
1364 /* Pre-allocate a few structures. */
1366 allocated_archive_names
= 10;
1367 archive_name_array
=
1368 xmalloc (sizeof (const char *) * allocated_archive_names
);
1372 /* System V fork+wait does not work if SIGCHLD is ignored. */
1373 signal (SIGCHLD
, SIG_DFL
);
1378 /* Decode options. */
1380 decode_options (argc
, argv
);
1381 name_init (argc
, argv
);
1383 /* Main command execution. */
1385 if (volno_file_option
)
1386 init_volume_number ();
1388 switch (subcommand_option
)
1390 case UNKNOWN_SUBCOMMAND
:
1392 _("You must specify one of the `-Acdtrux' options")));
1394 case CAT_SUBCOMMAND
:
1395 case UPDATE_SUBCOMMAND
:
1396 case APPEND_SUBCOMMAND
:
1400 case DELETE_SUBCOMMAND
:
1401 delete_archive_members ();
1404 case CREATE_SUBCOMMAND
:
1409 print_total_written ();
1412 case EXTRACT_SUBCOMMAND
:
1414 read_and (extract_archive
);
1416 /* FIXME: should extract_finish () even if an ordinary signal is
1422 case LIST_SUBCOMMAND
:
1423 read_and (list_archive
);
1426 case DIFF_SUBCOMMAND
:
1428 read_and (diff_archive
);
1432 if (check_links_option
)
1435 if (volno_file_option
)
1436 closeout_volume_number ();
1438 /* Dispose of allocated memory, and return. */
1440 free (archive_name_array
);
1443 if (stdlis
!= stderr
&& (ferror (stdlis
) || fclose (stdlis
) != 0))
1444 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1445 if (exit_status
== TAREXIT_FAILURE
)
1446 error (0, 0, _("Error exit delayed from previous errors"));
1447 if (ferror (stderr
) || fclose (stderr
) != 0)
1448 exit_status
= TAREXIT_FAILURE
;