1 /* A tar (tape archiver) program.
3 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001
4 Free Software Foundation, Inc.
6 Written by John Gilmore, starting 1985-08-25.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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. */
39 #include <print-copyr.h>
40 #include <localedir.h>
47 /* Local declarations. */
49 #ifndef DEFAULT_ARCHIVE
50 # define DEFAULT_ARCHIVE "tar.out"
53 #ifndef DEFAULT_BLOCKING
54 # define DEFAULT_BLOCKING 20
57 static void usage
PARAMS ((int)) __attribute__ ((noreturn
));
61 /* Name of option using stdin. */
62 static const char *stdin_used_by
;
64 /* Doesn't return if stdin already requested. */
66 request_stdin (const char *option
)
69 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
70 stdin_used_by
, option
));
72 stdin_used_by
= option
;
75 /* Returns true if and only if the user typed 'y' or 'Y'. */
77 confirm (const char *message_action
, const char *message_name
)
79 static FILE *confirm_file
;
80 static int confirm_file_EOF
;
84 if (archive
== 0 || stdin_used_by
)
86 confirm_file
= fopen (TTY_NAME
, "r");
88 open_fatal (TTY_NAME
);
97 fprintf (stdlis
, "%s %s?", message_action
, quote (message_name
));
101 int reply
= confirm_file_EOF
? EOF
: getc (confirm_file
);
104 for (character
= reply
;
106 character
= getc (confirm_file
))
107 if (character
== EOF
)
109 confirm_file_EOF
= 1;
110 fputc ('\n', stdlis
);
114 return reply
== 'y' || reply
== 'Y';
120 /* For long options that unconditionally set a single flag, we have getopt
121 do it. For the others, we share the code for the equivalent short
122 named option, the name of which is stored in the otherwise-unused `val'
123 field of the `struct option'; for long options that have no equivalent
124 short option, we use non-characters as pseudo short options,
125 starting at CHAR_MAX + 1 and going upwards. */
129 ANCHORED_OPTION
= CHAR_MAX
+ 1,
138 NO_IGNORE_CASE_OPTION
,
140 NO_WILDCARDS_MATCH_SLASH_OPTION
,
143 OVERWRITE_DIR_OPTION
,
150 USE_COMPRESS_PROGRAM_OPTION
,
153 WILDCARDS_MATCH_SLASH_OPTION
,
155 /* Some cleanup is being made in GNU tar long options. Using old names is
156 allowed for a while, but will also send a warning to stderr. Take old
157 names out in 1.14, or in summer 1997, whichever happens last. */
159 OBSOLETE_ABSOLUTE_NAMES
,
160 OBSOLETE_BLOCK_COMPRESS
,
161 OBSOLETE_BLOCKING_FACTOR
,
162 OBSOLETE_BLOCK_NUMBER
,
163 OBSOLETE_READ_FULL_RECORDS
,
165 OBSOLETE_VERSION_CONTROL
168 /* If nonzero, display usage information and exit. */
169 static int show_help
;
171 /* If nonzero, print the version on standard output and exit. */
172 static int show_version
;
174 static struct option long_options
[] =
176 {"absolute-names", no_argument
, 0, 'P'},
177 {"absolute-paths", no_argument
, 0, OBSOLETE_ABSOLUTE_NAMES
},
178 {"after-date", required_argument
, 0, 'N'},
179 {"anchored", no_argument
, 0, ANCHORED_OPTION
},
180 {"append", no_argument
, 0, 'r'},
181 {"atime-preserve", no_argument
, &atime_preserve_option
, 1},
182 {"backup", optional_argument
, 0, BACKUP_OPTION
},
183 {"block-compress", no_argument
, 0, OBSOLETE_BLOCK_COMPRESS
},
184 {"block-number", no_argument
, 0, 'R'},
185 {"block-size", required_argument
, 0, OBSOLETE_BLOCKING_FACTOR
},
186 {"blocking-factor", required_argument
, 0, 'b'},
187 {"bzip2", no_argument
, 0, 'j'},
188 {"catenate", no_argument
, 0, 'A'},
189 {"checkpoint", no_argument
, &checkpoint_option
, 1},
190 {"compare", no_argument
, 0, 'd'},
191 {"compress", no_argument
, 0, 'Z'},
192 {"concatenate", no_argument
, 0, 'A'},
193 {"confirmation", no_argument
, 0, 'w'},
194 /* FIXME: --selective as a synonym for --confirmation? */
195 {"create", no_argument
, 0, 'c'},
196 {"delete", no_argument
, 0, DELETE_OPTION
},
197 {"dereference", no_argument
, 0, 'h'},
198 {"diff", no_argument
, 0, 'd'},
199 {"directory", required_argument
, 0, 'C'},
200 {"exclude", required_argument
, 0, EXCLUDE_OPTION
},
201 {"exclude-from", required_argument
, 0, 'X'},
202 {"extract", no_argument
, 0, 'x'},
203 {"file", required_argument
, 0, 'f'},
204 {"files-from", required_argument
, 0, 'T'},
205 {"force-local", no_argument
, &force_local_option
, 1},
206 {"get", no_argument
, 0, 'x'},
207 {"group", required_argument
, 0, GROUP_OPTION
},
208 {"gunzip", no_argument
, 0, 'z'},
209 {"gzip", no_argument
, 0, 'z'},
210 {"help", no_argument
, &show_help
, 1},
211 {"ignore-case", no_argument
, 0, IGNORE_CASE_OPTION
},
212 {"ignore-failed-read", no_argument
, &ignore_failed_read_option
, 1},
213 {"ignore-zeros", no_argument
, 0, 'i'},
214 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
215 {"incremental", no_argument
, 0, 'G'},
216 {"info-script", required_argument
, 0, 'F'},
217 {"interactive", no_argument
, 0, 'w'},
218 {"keep-old-files", no_argument
, 0, 'k'},
219 {"label", required_argument
, 0, 'V'},
220 {"list", no_argument
, 0, 't'},
221 {"listed-incremental", required_argument
, 0, 'g'},
222 {"mode", required_argument
, 0, MODE_OPTION
},
223 {"modification-time", no_argument
, 0, OBSOLETE_TOUCH
},
224 {"multi-volume", no_argument
, 0, 'M'},
225 {"new-volume-script", required_argument
, 0, 'F'},
226 {"newer", required_argument
, 0, 'N'},
227 {"newer-mtime", required_argument
, 0, NEWER_MTIME_OPTION
},
228 {"null", no_argument
, 0, NULL_OPTION
},
229 {"no-anchored", no_argument
, 0, NO_ANCHORED_OPTION
},
230 {"no-ignore-case", no_argument
, 0, NO_IGNORE_CASE_OPTION
},
231 {"no-wildcards", no_argument
, 0, NO_WILDCARDS_OPTION
},
232 {"no-wildcards-match-slash", no_argument
, 0, NO_WILDCARDS_MATCH_SLASH_OPTION
},
233 {"no-recursion", no_argument
, &recursion_option
, 0},
234 {"no-same-owner", no_argument
, &same_owner_option
, -1},
235 {"no-same-permissions", no_argument
, &same_permissions_option
, -1},
236 {"numeric-owner", no_argument
, &numeric_owner_option
, 1},
237 {"old-archive", no_argument
, 0, 'o'},
238 {"one-file-system", no_argument
, 0, 'l'},
239 {"overwrite", no_argument
, 0, OVERWRITE_OPTION
},
240 {"overwrite-dir", no_argument
, 0, OVERWRITE_DIR_OPTION
},
241 {"owner", required_argument
, 0, OWNER_OPTION
},
242 {"portability", no_argument
, 0, 'o'},
243 {"posix", no_argument
, 0, POSIX_OPTION
},
244 {"preserve", no_argument
, 0, PRESERVE_OPTION
},
245 {"preserve-order", no_argument
, 0, 's'},
246 {"preserve-permissions", no_argument
, 0, 'p'},
247 {"recursion", no_argument
, &recursion_option
, FNM_LEADING_DIR
},
248 {"recursive-unlink", no_argument
, &recursive_unlink_option
, 1},
249 {"read-full-blocks", no_argument
, 0, OBSOLETE_READ_FULL_RECORDS
},
250 {"read-full-records", no_argument
, 0, 'B'},
251 /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
252 {"record-number", no_argument
, 0, OBSOLETE_BLOCK_NUMBER
},
253 {"record-size", required_argument
, 0, RECORD_SIZE_OPTION
},
254 {"remove-files", no_argument
, &remove_files_option
, 1},
255 {"rsh-command", required_argument
, 0, RSH_COMMAND_OPTION
},
256 {"same-order", no_argument
, 0, 's'},
257 {"same-owner", no_argument
, &same_owner_option
, 1},
258 {"same-permissions", no_argument
, 0, 'p'},
259 {"show-omitted-dirs", no_argument
, &show_omitted_dirs_option
, 1},
260 {"sparse", no_argument
, 0, 'S'},
261 {"starting-file", required_argument
, 0, 'K'},
262 {"suffix", required_argument
, 0, SUFFIX_OPTION
},
263 {"tape-length", required_argument
, 0, 'L'},
264 {"to-stdout", no_argument
, 0, 'O'},
265 {"totals", no_argument
, &totals_option
, 1},
266 {"touch", no_argument
, 0, 'm'},
267 {"uncompress", no_argument
, 0, 'Z'},
268 {"ungzip", no_argument
, 0, 'z'},
269 {"unlink-first", no_argument
, 0, 'U'},
270 {"update", no_argument
, 0, 'u'},
271 {"use-compress-program", required_argument
, 0, USE_COMPRESS_PROGRAM_OPTION
},
272 {"verbose", no_argument
, 0, 'v'},
273 {"verify", no_argument
, 0, 'W'},
274 {"version", no_argument
, &show_version
, 1},
275 {"version-control", required_argument
, 0, OBSOLETE_VERSION_CONTROL
},
276 {"volno-file", required_argument
, 0, VOLNO_FILE_OPTION
},
277 {"wildcards", no_argument
, 0, WILDCARDS_OPTION
},
278 {"wildcards-match-slash", no_argument
, 0, WILDCARDS_MATCH_SLASH_OPTION
},
283 /* Print a usage message and exit with STATUS. */
287 if (status
!= TAREXIT_SUCCESS
)
288 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
293 GNU `tar' saves many files together into a single tape or disk archive, and\n\
294 can restore individual files from the archive.\n"),
296 printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
299 %s -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
300 %s -tvf archive.tar # List all files in archive.tar verbosely.\n\
301 %s -xf archive.tar # Extract all files from archive.tar.\n"),
302 program_name
, program_name
, program_name
, program_name
);
305 If a long option shows an argument as mandatory, then it is mandatory\n\
306 for the equivalent short option also. Similarly for optional arguments.\n"),
310 Main operation mode:\n\
311 -t, --list list the contents of an archive\n\
312 -x, --extract, --get extract files from an archive\n\
313 -c, --create create a new archive\n\
314 -d, --diff, --compare find differences between archive and file system\n\
315 -r, --append append files to the end of an archive\n\
316 -u, --update only append files newer than copy in archive\n\
317 -A, --catenate append tar files to an archive\n\
318 --concatenate same as -A\n\
319 --delete delete from the archive (not on mag tapes!)\n"),
323 Operation modifiers:\n\
324 -W, --verify attempt to verify the archive after writing it\n\
325 --remove-files remove files after adding them to the archive\n\
326 -k, --keep-old-files don't replace existing files when extracting\n\
327 --overwrite overwrite existing files when extracting\n\
328 --overwrite-dir overwrite directory metadata when extracting\n\
329 -U, --unlink-first remove each file prior to extracting over it\n\
330 --recursive-unlink empty hierarchies prior to extracting directory\n\
331 -S, --sparse handle sparse files efficiently\n\
332 -O, --to-stdout extract files to standard output\n\
333 -G, --incremental handle old GNU-format incremental backup\n\
334 -g, --listed-incremental=FILE\n\
335 handle new GNU-format incremental backup\n\
336 --ignore-failed-read do not exit with nonzero on unreadable files\n"),
340 Handling of file attributes:\n\
341 --owner=NAME force NAME as owner for added files\n\
342 --group=NAME force NAME as group for added files\n\
343 --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
344 --atime-preserve don't change access times on dumped files\n\
345 -m, --modification-time don't extract file modified time\n\
346 --same-owner try extracting files with the same ownership\n\
347 --no-same-owner extract files as yourself\n\
348 --numeric-owner always use numbers for user/group names\n\
349 -p, --same-permissions extract permissions information\n\
350 --no-same-permissions do not extract permissions information\n\
351 --preserve-permissions same as -p\n\
352 -s, --same-order sort names to extract to match archive\n\
353 --preserve-order same as -s\n\
354 --preserve same as both -p and -s\n"),
358 Device selection and switching:\n\
359 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
360 --force-local archive file is local even if has a colon\n\
361 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
362 -[0-7][lmh] specify drive and density\n\
363 -M, --multi-volume create/list/extract multi-volume archive\n\
364 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
365 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
366 --new-volume-script=FILE same as -F FILE\n\
367 --volno-file=FILE use/update the volume number in FILE\n"),
372 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
373 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
374 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
375 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
379 Archive format selection:\n\
380 -V, --label=NAME create archive with volume name NAME\n\
381 PATTERN at list/extract time, a globbing PATTERN\n\
382 -o, --old-archive, --portability write a V7 format archive\n\
383 --posix write a POSIX format archive\n\
384 -j, --bzip2 filter the archive through bzip2\n\
385 -z, --gzip, --ungzip filter the archive through gzip\n\
386 -Z, --compress, --uncompress filter the archive through compress\n\
387 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
391 Local file selection:\n\
392 -C, --directory=DIR change to directory DIR\n\
393 -T, --files-from=NAME get names to extract or create from file NAME\n\
394 --null -T reads null-terminated names, disable -C\n\
395 --exclude=PATTERN exclude files, given as a PATTERN\n\
396 -X, --exclude-from=FILE exclude patterns listed in FILE\n\
397 --anchored exclude patterns match file name start (default)\n\
398 --no-anchored exclude patterns match after any /\n\
399 --ignore-case exclusion ignores case\n\
400 --no-ignore-case exclusion is case sensitive (default)\n\
401 --wildcards exclude patterns use wildcards (default)\n\
402 --no-wildcards exclude patterns are plain strings\n\
403 --wildcards-match-slash exclude pattern wildcards match '/' (default)\n\
404 --no-wildcards-match-slash exclude pattern wildcards do not match '/'\n\
405 -P, --absolute-names don't strip leading `/'s from file names\n\
406 -h, --dereference dump instead the files symlinks point to\n\
407 --no-recursion avoid descending automatically in directories\n\
408 -l, --one-file-system stay in local file system when creating archive\n\
409 -K, --starting-file=NAME begin at file NAME in the archive\n"),
413 -N, --newer=DATE only store files newer than DATE\n\
414 --newer-mtime=DATE compare date and time when data changed only\n\
415 --after-date=DATE same as -N\n"),
419 --backup[=CONTROL] backup before removal, choose version control\n\
420 --suffix=SUFFIX backup before removal, override usual suffix\n"),
424 Informative output:\n\
425 --help print this help, then exit\n\
426 --version print tar program version number, then exit\n\
427 -v, --verbose verbosely list files processed\n\
428 --checkpoint print directory names while reading the archive\n\
429 --totals print total bytes written while creating archive\n\
430 -R, --block-number show block number within archive with each message\n\
431 -w, --interactive ask for confirmation for every action\n\
432 --confirmation same as -w\n"),
436 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
437 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
439 t, numbered make numbered backups\n\
440 nil, existing numbered if numbered backups exist, simple otherwise\n\
441 never, simple always make simple backups\n"),
445 GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
446 is set in the environment, GNU extensions are disallowed with `--posix'.\n\
447 Support for POSIX is only partially implemented, don't count on it yet.\n\
448 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; DATE may be a textual date\n\
449 or a file name starting with `/' or `.', in which case the file's date is used.\n\
450 *This* `tar' defaults to `-f%s -b%d'.\n"),
451 DEFAULT_ARCHIVE
, DEFAULT_BLOCKING
);
452 fputs (_("\nReport bugs to <bug-tar@gnu.org>.\n"), stdout
);
457 /* Parse the options for tar. */
459 /* Available option letters are DEHIJQY and aenqy. Some are reserved:
461 e exit immediately with a nonzero exit status if unexpected errors occur
462 E use extended headers (draft POSIX headers, that is)
463 I same as T (for compatibility with Solaris tar)
464 n the archive is quickly seekable, so don't worry about random seeks
465 q stop after extracting the first occurrence of the named file
466 y per-file gzip compression
467 Y per-block gzip compression */
469 #define OPTION_STRING \
470 "-01234567ABC:F:GIK:L:MN:OPRST:UV:WX:Zb:cdf:g:hijklmoprstuvwxyz"
473 set_subcommand_option (enum subcommand subcommand
)
475 if (subcommand_option
!= UNKNOWN_SUBCOMMAND
476 && subcommand_option
!= subcommand
)
478 _("You may not specify more than one `-Acdtrux' option")));
480 subcommand_option
= subcommand
;
484 set_use_compress_program_option (const char *string
)
486 if (use_compress_program_option
&& strcmp (use_compress_program_option
, string
) != 0)
487 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
489 use_compress_program_option
= string
;
493 decode_options (int argc
, char **argv
)
495 int optchar
; /* option letter */
496 int input_files
; /* number of input files */
497 const char *backup_suffix_string
;
498 const char *version_control_string
= 0;
499 int exclude_options
= EXCLUDE_WILDCARDS
;
501 /* Set some default option values. */
503 subcommand_option
= UNKNOWN_SUBCOMMAND
;
504 archive_format
= DEFAULT_FORMAT
;
505 blocking_factor
= DEFAULT_BLOCKING
;
506 record_size
= DEFAULT_BLOCKING
* BLOCKSIZE
;
507 excluded
= new_exclude ();
508 newer_mtime_option
= TYPE_MINIMUM (time_t);
509 recursion_option
= FNM_LEADING_DIR
;
514 backup_suffix_string
= getenv ("SIMPLE_BACKUP_SUFFIX");
516 /* Convert old-style tar call by exploding option element and rearranging
517 options accordingly. */
519 if (argc
> 1 && argv
[1][0] != '-')
521 int new_argc
; /* argc value for rearranged arguments */
522 char **new_argv
; /* argv value for rearranged arguments */
523 char *const *in
; /* cursor into original argv */
524 char **out
; /* cursor into rearranged argv */
525 const char *letter
; /* cursor into old option letters */
526 char buffer
[3]; /* constructed option buffer */
527 const char *cursor
; /* cursor in OPTION_STRING */
529 /* Initialize a constructed option. */
534 /* Allocate a new argument array, and copy program name in it. */
536 new_argc
= argc
- 1 + strlen (argv
[1]);
537 new_argv
= xmalloc (new_argc
* sizeof (char *));
542 /* Copy each old letter option as a separate option, and have the
543 corresponding argument moved next to it. */
545 for (letter
= *in
++; *letter
; letter
++)
548 *out
++ = xstrdup (buffer
);
549 cursor
= strchr (OPTION_STRING
, *letter
);
550 if (cursor
&& cursor
[1] == ':')
552 if (in
< argv
+ argc
)
555 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
560 /* Copy all remaining options. */
562 while (in
< argv
+ argc
)
565 /* Replace the old option list by the new one. */
571 /* Parse all options and non-options as they appear. */
575 prepend_default_options (getenv ("TAR_OPTIONS"), &argc
, &argv
);
577 while (optchar
= getopt_long (argc
, argv
, OPTION_STRING
, long_options
, 0),
582 usage (TAREXIT_FAILURE
);
588 /* File name or non-parsed option, because of RETURN_IN_ORDER
589 ordering triggered by the leading dash in OPTION_STRING. */
596 set_subcommand_option (CAT_SUBCOMMAND
);
599 case OBSOLETE_BLOCK_COMPRESS
:
600 WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
603 case OBSOLETE_BLOCKING_FACTOR
:
604 WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
610 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
611 && u
== (blocking_factor
= u
)
612 && 0 < blocking_factor
613 && u
== (record_size
= u
* BLOCKSIZE
) / BLOCKSIZE
))
614 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
615 _("Invalid blocking factor")));
619 case OBSOLETE_READ_FULL_RECORDS
:
621 _("Obsolete option name replaced by --read-full-records")));
625 /* Try to reblock input records. For reading 4.2BSD pipes. */
627 /* It would surely make sense to exchange -B and -R, but it seems
628 that -B has been used for a long while in Sun tar ans most
629 BSD-derived systems. This is a consequence of the block/record
630 terminology confusion. */
632 read_full_records_option
= 1;
636 set_subcommand_option (CREATE_SUBCOMMAND
);
645 set_subcommand_option (DIFF_SUBCOMMAND
);
649 if (archive_names
== allocated_archive_names
)
651 allocated_archive_names
*= 2;
653 xrealloc (archive_name_array
,
654 sizeof (const char *) * allocated_archive_names
);
656 archive_name_array
[archive_names
++] = optarg
;
660 /* Since -F is only useful with -M, make it implied. Run this
661 script at the end of each tape. */
663 info_script_option
= optarg
;
664 multi_volume_option
= 1;
668 listed_incremental_option
= optarg
;
669 after_date_option
= 1;
673 /* We are making an incremental dump (FIXME: are we?); save
674 directories at the beginning of the archive, and include in each
675 directory its contents. */
677 incremental_option
= 1;
681 /* Follow symbolic links. */
683 dereference_option
= 1;
687 /* Ignore zero blocks (eofs). This can't be the default,
688 because Unix tar writes two blocks of zeros, then pads out
689 the record with garbage. */
691 ignore_zeros_option
= 1;
696 _("Warning: the -I option is not supported;"
697 " perhaps you meant -j or -T?")));
701 set_use_compress_program_option ("bzip2");
705 /* Don't replace existing files. */
706 old_files_option
= KEEP_OLD_FILES
;
710 starting_file_option
= 1;
715 /* When dumping directories, don't dump files/subdirectories
716 that are on other filesystems. */
718 one_file_system_option
= 1;
724 if (xstrtoumax (optarg
, 0, 10, &u
, "") != LONGINT_OK
)
725 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
726 _("Invalid tape length")));
727 tape_length_option
= 1024 * (tarlong
) u
;
728 multi_volume_option
= 1;
733 WARN ((0, 0, _("Obsolete option name replaced by --touch")));
741 /* Make multivolume archive: when we can't write any more into
742 the archive, re-open it, and continue writing. */
744 multi_volume_option
= 1;
749 after_date_option
= 1;
752 case NEWER_MTIME_OPTION
:
753 if (newer_mtime_option
!= TYPE_MINIMUM (time_t))
754 USAGE_ERROR ((0, 0, _("More than one threshold date")));
756 if (FILESYSTEM_PREFIX_LEN (optarg
) != 0
761 if (deref_stat (dereference_option
, optarg
, &st
) != 0)
764 USAGE_ERROR ((0, 0, _("Date file not found")));
766 newer_mtime_option
= st
.st_mtime
;
770 newer_mtime_option
= get_date (optarg
, 0);
771 if (newer_mtime_option
== (time_t) -1)
772 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
773 tartime (newer_mtime_option
), quote (optarg
)));
777 #endif /* not MSDOS */
780 if (archive_format
== DEFAULT_FORMAT
)
781 archive_format
= V7_FORMAT
;
782 else if (archive_format
!= V7_FORMAT
)
783 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
787 to_stdout_option
= 1;
791 same_permissions_option
= 1;
794 case OBSOLETE_ABSOLUTE_NAMES
:
795 WARN ((0, 0, _("Obsolete option name replaced by --absolute-names")));
799 absolute_names_option
= 1;
803 set_subcommand_option (APPEND_SUBCOMMAND
);
806 case OBSOLETE_BLOCK_NUMBER
:
807 WARN ((0, 0, _("Obsolete option name replaced by --block-number")));
811 /* Print block numbers for debugging bad tar archives. */
813 /* It would surely make sense to exchange -B and -R, but it seems
814 that -B has been used for a long while in Sun tar ans most
815 BSD-derived systems. This is a consequence of the block/record
816 terminology confusion. */
818 block_number_option
= 1;
822 /* Names to extr are sorted. */
824 same_order_option
= 1;
832 set_subcommand_option (LIST_SUBCOMMAND
);
837 files_from_option
= optarg
;
841 set_subcommand_option (UPDATE_SUBCOMMAND
);
845 old_files_option
= UNLINK_FIRST_OLD_FILES
;
853 volume_label_option
= optarg
;
857 interactive_option
= 1;
865 set_subcommand_option (EXTRACT_SUBCOMMAND
);
869 if (add_exclude_file (add_exclude
, excluded
, optarg
,
870 exclude_options
| recursion_option
, '\n')
874 FATAL_ERROR ((0, e
, "%s", quotearg_colon (optarg
)));
880 _("Warning: the -y option is not supported;"
881 " perhaps you meant -j?")));
885 set_use_compress_program_option ("gzip");
889 set_use_compress_program_option ("compress");
892 case OBSOLETE_VERSION_CONTROL
:
893 WARN ((0, 0, _("Obsolete option name replaced by --backup")));
896 case ANCHORED_OPTION
:
897 exclude_options
|= EXCLUDE_ANCHORED
;
903 version_control_string
= optarg
;
907 set_subcommand_option (DELETE_SUBCOMMAND
);
911 add_exclude (excluded
, optarg
, exclude_options
| recursion_option
);
914 case IGNORE_CASE_OPTION
:
915 exclude_options
|= FNM_CASEFOLD
;
919 if (! (strlen (optarg
) < GNAME_FIELD_SIZE
920 && gname_to_gid (optarg
, &group_option
)))
923 if (xstrtoumax (optarg
, 0, 10, &g
, "") == LONGINT_OK
927 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
928 _("%s: Invalid group")));
934 = mode_compile (optarg
,
935 MODE_MASK_EQUALS
| MODE_MASK_PLUS
| MODE_MASK_MINUS
);
936 if (mode_option
== MODE_INVALID
)
937 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
938 if (mode_option
== MODE_MEMORY_EXHAUSTED
)
942 case NO_ANCHORED_OPTION
:
943 exclude_options
&= ~ EXCLUDE_ANCHORED
;
946 case NO_IGNORE_CASE_OPTION
:
947 exclude_options
&= ~ FNM_CASEFOLD
;
950 case NO_WILDCARDS_OPTION
:
951 exclude_options
&= ~ EXCLUDE_WILDCARDS
;
954 case NO_WILDCARDS_MATCH_SLASH_OPTION
:
955 exclude_options
|= FNM_FILE_NAME
;
959 filename_terminator
= '\0';
962 case OVERWRITE_OPTION
:
963 old_files_option
= OVERWRITE_OLD_FILES
;
966 case OVERWRITE_DIR_OPTION
:
967 old_files_option
= OVERWRITE_OLD_DIRS
;
971 if (! (strlen (optarg
) < UNAME_FIELD_SIZE
972 && uname_to_uid (optarg
, &owner_option
)))
975 if (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
979 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
980 _("Invalid owner")));
985 #if OLDGNU_COMPATIBILITY
986 if (archive_format
== DEFAULT_FORMAT
)
987 archive_format
= GNU_FORMAT
;
988 else if (archive_format
!= GNU_FORMAT
)
989 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
991 if (archive_format
== DEFAULT_FORMAT
)
992 archive_format
= POSIX_FORMAT
;
993 else if (archive_format
!= POSIX_FORMAT
)
994 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
998 case PRESERVE_OPTION
:
999 same_permissions_option
= 1;
1000 same_order_option
= 1;
1003 case RECORD_SIZE_OPTION
:
1006 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
1007 && u
== (size_t) u
))
1008 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1009 _("Invalid record size")));
1011 if (record_size
% BLOCKSIZE
!= 0)
1012 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
1014 blocking_factor
= record_size
/ BLOCKSIZE
;
1018 case RSH_COMMAND_OPTION
:
1019 rsh_command_option
= optarg
;
1024 backup_suffix_string
= optarg
;
1027 case USE_COMPRESS_PROGRAM_OPTION
:
1028 set_use_compress_program_option (optarg
);
1031 case VOLNO_FILE_OPTION
:
1032 volno_file_option
= optarg
;
1035 case WILDCARDS_OPTION
:
1036 exclude_options
|= EXCLUDE_WILDCARDS
;
1039 case WILDCARDS_MATCH_SLASH_OPTION
:
1040 exclude_options
&= ~ FNM_FILE_NAME
;
1052 #ifdef DEVICE_PREFIX
1054 int device
= optchar
- '0';
1056 static char buf
[sizeof DEVICE_PREFIX
+ 10];
1059 density
= getopt_long (argc
, argv
, "lmh", 0, 0);
1060 strcpy (buf
, DEVICE_PREFIX
);
1061 cursor
= buf
+ strlen (buf
);
1063 #ifdef DENSITY_LETTER
1065 sprintf (cursor
, "%d%c", device
, density
);
1067 #else /* not DENSITY_LETTER */
1094 usage (TAREXIT_FAILURE
);
1096 sprintf (cursor
, "%d", device
);
1098 #endif /* not DENSITY_LETTER */
1100 if (archive_names
== allocated_archive_names
)
1102 allocated_archive_names
*= 2;
1103 archive_name_array
=
1104 xrealloc (archive_name_array
,
1105 sizeof (const char *) * allocated_archive_names
);
1107 archive_name_array
[archive_names
++] = buf
;
1109 /* FIXME: How comes this works for many archives when buf is
1114 #else /* not DEVICE_PREFIX */
1117 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1119 #endif /* not DEVICE_PREFIX */
1122 /* Handle operands after any "--" argument. */
1123 for (; optind
< argc
; optind
++)
1125 name_add (argv
[optind
]);
1129 /* Process trivial options. */
1133 printf ("tar (GNU %s) %s\n", PACKAGE
, VERSION
);
1134 print_copyright ("2001 Free Software Foundation, Inc.");
1136 This program comes with NO WARRANTY, to the extent permitted by law.\n\
1137 You may redistribute it under the terms of the GNU General Public License;\n\
1138 see the file named COPYING for details."));
1140 puts (_("Written by John Gilmore and Jay Fenlason."));
1142 exit (TAREXIT_SUCCESS
);
1146 usage (TAREXIT_SUCCESS
);
1148 /* Derive option values and check option consistency. */
1150 if (archive_format
== DEFAULT_FORMAT
)
1152 #if OLDGNU_COMPATIBILITY
1153 archive_format
= OLDGNU_FORMAT
;
1155 archive_format
= GNU_FORMAT
;
1159 if (archive_format
== GNU_FORMAT
&& getenv ("POSIXLY_CORRECT"))
1160 archive_format
= POSIX_FORMAT
;
1162 if ((volume_label_option
1163 || incremental_option
|| multi_volume_option
|| sparse_option
)
1164 && archive_format
!= OLDGNU_FORMAT
&& archive_format
!= GNU_FORMAT
)
1166 _("GNU features wanted on incompatible archive format")));
1168 if (archive_names
== 0)
1170 /* If no archive file name given, try TAPE from the environment, or
1171 else, DEFAULT_ARCHIVE from the configuration process. */
1174 archive_name_array
[0] = getenv ("TAPE");
1175 if (! archive_name_array
[0])
1176 archive_name_array
[0] = DEFAULT_ARCHIVE
;
1179 /* Allow multiple archives only with `-M'. */
1181 if (archive_names
> 1 && !multi_volume_option
)
1183 _("Multiple archive files requires `-M' option")));
1185 if (listed_incremental_option
1186 && newer_mtime_option
!= TYPE_MINIMUM (time_t))
1188 _("Cannot combine --listed-incremental with --newer")));
1190 if (volume_label_option
)
1192 size_t volume_label_max_len
=
1193 (sizeof current_header
->header
.name
1194 - 1 /* for trailing '\0' */
1195 - (multi_volume_option
1196 ? (sizeof " Volume "
1197 - 1 /* for null at end of " Volume " */
1198 + INT_STRLEN_BOUND (int) /* for volume number */
1199 - 1 /* for sign, as 0 <= volno */)
1201 if (volume_label_max_len
< strlen (volume_label_option
))
1203 _("%s: Volume label is too long (limit is %lu bytes)"),
1204 quotearg_colon (volume_label_option
),
1205 (unsigned long) volume_label_max_len
));
1208 /* If ready to unlink hierarchies, so we are for simpler files. */
1209 if (recursive_unlink_option
)
1210 old_files_option
= UNLINK_FIRST_OLD_FILES
;
1212 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1213 explicit or implied, is used correctly. */
1215 switch (subcommand_option
)
1217 case CREATE_SUBCOMMAND
:
1218 if (input_files
== 0 && !files_from_option
)
1220 _("Cowardly refusing to create an empty archive")));
1223 case EXTRACT_SUBCOMMAND
:
1224 case LIST_SUBCOMMAND
:
1225 case DIFF_SUBCOMMAND
:
1226 for (archive_name_cursor
= archive_name_array
;
1227 archive_name_cursor
< archive_name_array
+ archive_names
;
1228 archive_name_cursor
++)
1229 if (!strcmp (*archive_name_cursor
, "-"))
1230 request_stdin ("-f");
1233 case CAT_SUBCOMMAND
:
1234 case UPDATE_SUBCOMMAND
:
1235 case APPEND_SUBCOMMAND
:
1236 for (archive_name_cursor
= archive_name_array
;
1237 archive_name_cursor
< archive_name_array
+ archive_names
;
1238 archive_name_cursor
++)
1239 if (!strcmp (*archive_name_cursor
, "-"))
1241 _("Options `-Aru' are incompatible with `-f -'")));
1247 archive_name_cursor
= archive_name_array
;
1249 /* Prepare for generating backup names. */
1251 if (backup_suffix_string
)
1252 simple_backup_suffix
= xstrdup (backup_suffix_string
);
1255 backup_type
= xget_version ("--backup", version_control_string
);
1260 /* Main routine for tar. */
1262 main (int argc
, char **argv
)
1264 #if HAVE_CLOCK_GETTIME
1265 if (clock_gettime (CLOCK_REALTIME
, &start_timespec
) != 0)
1267 start_time
= time (0);
1268 program_name
= argv
[0];
1269 setlocale (LC_ALL
, "");
1270 bindtextdomain (PACKAGE
, LOCALEDIR
);
1271 textdomain (PACKAGE
);
1273 exit_status
= TAREXIT_SUCCESS
;
1274 filename_terminator
= '\n';
1275 set_quoting_style (0, escape_quoting_style
);
1277 /* Pre-allocate a few structures. */
1279 allocated_archive_names
= 10;
1280 archive_name_array
=
1281 xmalloc (sizeof (const char *) * allocated_archive_names
);
1285 /* System V fork+wait does not work if SIGCHLD is ignored. */
1286 signal (SIGCHLD
, SIG_DFL
);
1291 /* Decode options. */
1293 decode_options (argc
, argv
);
1294 name_init (argc
, argv
);
1296 /* Main command execution. */
1298 if (volno_file_option
)
1299 init_volume_number ();
1301 switch (subcommand_option
)
1303 case UNKNOWN_SUBCOMMAND
:
1305 _("You must specify one of the `-Acdtrux' options")));
1307 case CAT_SUBCOMMAND
:
1308 case UPDATE_SUBCOMMAND
:
1309 case APPEND_SUBCOMMAND
:
1313 case DELETE_SUBCOMMAND
:
1314 delete_archive_members ();
1317 case CREATE_SUBCOMMAND
:
1322 print_total_written ();
1325 case EXTRACT_SUBCOMMAND
:
1327 read_and (extract_archive
);
1331 case LIST_SUBCOMMAND
:
1332 read_and (list_archive
);
1335 case DIFF_SUBCOMMAND
:
1337 read_and (diff_archive
);
1341 if (volno_file_option
)
1342 closeout_volume_number ();
1344 /* Dispose of allocated memory, and return. */
1346 free (archive_name_array
);
1349 if (stdlis
== stdout
&& (ferror (stdout
) || fclose (stdout
) != 0))
1350 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1351 if (exit_status
== TAREXIT_FAILURE
)
1352 error (0, 0, _("Error exit delayed from previous errors"));