1 /* A tar (tape archiver) program.
3 Copyright 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001 Free
4 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 <localedir.h>
46 /* Local declarations. */
48 #ifndef DEFAULT_ARCHIVE
49 # define DEFAULT_ARCHIVE "tar.out"
52 #ifndef DEFAULT_BLOCKING
53 # define DEFAULT_BLOCKING 20
56 static void usage
PARAMS ((int)) __attribute__ ((noreturn
));
60 /* Name of option using stdin. */
61 static const char *stdin_used_by
;
63 /* Doesn't return if stdin already requested. */
65 request_stdin (const char *option
)
68 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
69 stdin_used_by
, option
));
71 stdin_used_by
= option
;
74 /* Returns true if and only if the user typed 'y' or 'Y'. */
76 confirm (const char *message_action
, const char *message_name
)
78 static FILE *confirm_file
;
79 static int confirm_file_EOF
;
83 if (archive
== 0 || stdin_used_by
)
85 confirm_file
= fopen (TTY_NAME
, "r");
87 open_fatal (TTY_NAME
);
96 fprintf (stdlis
, "%s %s?", message_action
, quote (message_name
));
100 int reply
= confirm_file_EOF
? EOF
: getc (confirm_file
);
103 for (character
= reply
;
105 character
= getc (confirm_file
))
106 if (character
== EOF
)
108 confirm_file_EOF
= 1;
109 fputc ('\n', stdlis
);
113 return reply
== 'y' || reply
== 'Y';
119 /* For long options that unconditionally set a single flag, we have getopt
120 do it. For the others, we share the code for the equivalent short
121 named option, the name of which is stored in the otherwise-unused `val'
122 field of the `struct option'; for long options that have no equivalent
123 short option, we use non-characters as pseudo short options,
124 starting at CHAR_MAX + 1 and going upwards. */
128 ANCHORED_OPTION
= CHAR_MAX
+ 1,
137 NO_IGNORE_CASE_OPTION
,
139 NO_WILDCARDS_MATCH_SLASH_OPTION
,
148 USE_COMPRESS_PROGRAM_OPTION
,
151 WILDCARDS_MATCH_SLASH_OPTION
,
153 /* Some cleanup is being made in GNU tar long options. Using old names is
154 allowed for a while, but will also send a warning to stderr. Take old
155 names out in 1.14, or in summer 1997, whichever happens last. */
157 OBSOLETE_ABSOLUTE_NAMES
,
158 OBSOLETE_BLOCK_COMPRESS
,
159 OBSOLETE_BLOCKING_FACTOR
,
160 OBSOLETE_BLOCK_NUMBER
,
161 OBSOLETE_READ_FULL_RECORDS
,
163 OBSOLETE_VERSION_CONTROL
166 /* If nonzero, display usage information and exit. */
167 static int show_help
;
169 /* If nonzero, print the version on standard output and exit. */
170 static int show_version
;
172 static struct option long_options
[] =
174 {"absolute-names", no_argument
, 0, 'P'},
175 {"absolute-paths", no_argument
, 0, OBSOLETE_ABSOLUTE_NAMES
},
176 {"after-date", required_argument
, 0, 'N'},
177 {"anchored", no_argument
, 0, ANCHORED_OPTION
},
178 {"append", no_argument
, 0, 'r'},
179 {"atime-preserve", no_argument
, &atime_preserve_option
, 1},
180 {"backup", optional_argument
, 0, BACKUP_OPTION
},
181 {"block-compress", no_argument
, 0, OBSOLETE_BLOCK_COMPRESS
},
182 {"block-number", no_argument
, 0, 'R'},
183 {"block-size", required_argument
, 0, OBSOLETE_BLOCKING_FACTOR
},
184 {"blocking-factor", required_argument
, 0, 'b'},
185 {"bzip2", no_argument
, 0, 'j'},
186 {"catenate", no_argument
, 0, 'A'},
187 {"checkpoint", no_argument
, &checkpoint_option
, 1},
188 {"compare", no_argument
, 0, 'd'},
189 {"compress", no_argument
, 0, 'Z'},
190 {"concatenate", no_argument
, 0, 'A'},
191 {"confirmation", no_argument
, 0, 'w'},
192 /* FIXME: --selective as a synonym for --confirmation? */
193 {"create", no_argument
, 0, 'c'},
194 {"delete", no_argument
, 0, DELETE_OPTION
},
195 {"dereference", no_argument
, 0, 'h'},
196 {"diff", no_argument
, 0, 'd'},
197 {"directory", required_argument
, 0, 'C'},
198 {"exclude", required_argument
, 0, EXCLUDE_OPTION
},
199 {"exclude-from", required_argument
, 0, 'X'},
200 {"extract", no_argument
, 0, 'x'},
201 {"file", required_argument
, 0, 'f'},
202 {"files-from", required_argument
, 0, 'T'},
203 {"force-local", no_argument
, &force_local_option
, 1},
204 {"get", no_argument
, 0, 'x'},
205 {"group", required_argument
, 0, GROUP_OPTION
},
206 {"gunzip", no_argument
, 0, 'z'},
207 {"gzip", no_argument
, 0, 'z'},
208 {"help", no_argument
, &show_help
, 1},
209 {"ignore-case", no_argument
, 0, IGNORE_CASE_OPTION
},
210 {"ignore-failed-read", no_argument
, &ignore_failed_read_option
, 1},
211 {"ignore-zeros", no_argument
, 0, 'i'},
212 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
213 {"incremental", no_argument
, 0, 'G'},
214 {"info-script", required_argument
, 0, 'F'},
215 {"interactive", no_argument
, 0, 'w'},
216 {"keep-old-files", no_argument
, 0, 'k'},
217 {"label", required_argument
, 0, 'V'},
218 {"list", no_argument
, 0, 't'},
219 {"listed-incremental", required_argument
, 0, 'g'},
220 {"mode", required_argument
, 0, MODE_OPTION
},
221 {"modification-time", no_argument
, 0, OBSOLETE_TOUCH
},
222 {"multi-volume", no_argument
, 0, 'M'},
223 {"new-volume-script", required_argument
, 0, 'F'},
224 {"newer", required_argument
, 0, 'N'},
225 {"newer-mtime", required_argument
, 0, NEWER_MTIME_OPTION
},
226 {"null", no_argument
, 0, NULL_OPTION
},
227 {"no-anchored", no_argument
, 0, NO_ANCHORED_OPTION
},
228 {"no-ignore-case", no_argument
, 0, NO_IGNORE_CASE_OPTION
},
229 {"no-wildcards", no_argument
, 0, NO_WILDCARDS_OPTION
},
230 {"no-wildcards-match-slash", no_argument
, 0, NO_WILDCARDS_MATCH_SLASH_OPTION
},
231 {"no-recursion", no_argument
, &recursion_option
, 0},
232 {"no-same-owner", no_argument
, &same_owner_option
, -1},
233 {"no-same-permissions", no_argument
, &same_permissions_option
, -1},
234 {"numeric-owner", no_argument
, &numeric_owner_option
, 1},
235 {"old-archive", no_argument
, 0, 'o'},
236 {"one-file-system", no_argument
, 0, 'l'},
237 {"overwrite", no_argument
, 0, OVERWRITE_OPTION
},
238 {"owner", required_argument
, 0, OWNER_OPTION
},
239 {"portability", no_argument
, 0, 'o'},
240 {"posix", no_argument
, 0, POSIX_OPTION
},
241 {"preserve", no_argument
, 0, PRESERVE_OPTION
},
242 {"preserve-order", no_argument
, 0, 's'},
243 {"preserve-permissions", no_argument
, 0, 'p'},
244 {"recursion", no_argument
, &recursion_option
, FNM_LEADING_DIR
},
245 {"recursive-unlink", no_argument
, &recursive_unlink_option
, 1},
246 {"read-full-blocks", no_argument
, 0, OBSOLETE_READ_FULL_RECORDS
},
247 {"read-full-records", no_argument
, 0, 'B'},
248 /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
249 {"record-number", no_argument
, 0, OBSOLETE_BLOCK_NUMBER
},
250 {"record-size", required_argument
, 0, RECORD_SIZE_OPTION
},
251 {"remove-files", no_argument
, &remove_files_option
, 1},
252 {"rsh-command", required_argument
, 0, RSH_COMMAND_OPTION
},
253 {"same-order", no_argument
, 0, 's'},
254 {"same-owner", no_argument
, &same_owner_option
, 1},
255 {"same-permissions", no_argument
, 0, 'p'},
256 {"show-omitted-dirs", no_argument
, &show_omitted_dirs_option
, 1},
257 {"sparse", no_argument
, 0, 'S'},
258 {"starting-file", required_argument
, 0, 'K'},
259 {"suffix", required_argument
, 0, SUFFIX_OPTION
},
260 {"tape-length", required_argument
, 0, 'L'},
261 {"to-stdout", no_argument
, 0, 'O'},
262 {"totals", no_argument
, &totals_option
, 1},
263 {"touch", no_argument
, 0, 'm'},
264 {"uncompress", no_argument
, 0, 'Z'},
265 {"ungzip", no_argument
, 0, 'z'},
266 {"unlink-first", no_argument
, 0, 'U'},
267 {"update", no_argument
, 0, 'u'},
268 {"use-compress-program", required_argument
, 0, USE_COMPRESS_PROGRAM_OPTION
},
269 {"verbose", no_argument
, 0, 'v'},
270 {"verify", no_argument
, 0, 'W'},
271 {"version", no_argument
, &show_version
, 1},
272 {"version-control", required_argument
, 0, OBSOLETE_VERSION_CONTROL
},
273 {"volno-file", required_argument
, 0, VOLNO_FILE_OPTION
},
274 {"wildcards", no_argument
, 0, WILDCARDS_OPTION
},
275 {"wildcards-match-slash", no_argument
, 0, WILDCARDS_MATCH_SLASH_OPTION
},
280 /* Print a usage message and exit with STATUS. */
284 if (status
!= TAREXIT_SUCCESS
)
285 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
290 GNU `tar' saves many files together into a single tape or disk archive, and\n\
291 can restore individual files from the archive.\n"),
293 printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
296 %s -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
297 %s -tvf archive.tar # List all files in archive.tar verbosely.\n\
298 %s -xf archive.tar # Extract all files from archive.tar.\n"),
299 program_name
, program_name
, program_name
, program_name
);
302 If a long option shows an argument as mandatory, then it is mandatory\n\
303 for the equivalent short option also. Similarly for optional arguments.\n"),
307 Main operation mode:\n\
308 -t, --list list the contents of an archive\n\
309 -x, --extract, --get extract files from an archive\n\
310 -c, --create create a new archive\n\
311 -d, --diff, --compare find differences between archive and file system\n\
312 -r, --append append files to the end of an archive\n\
313 -u, --update only append files newer than copy in archive\n\
314 -A, --catenate append tar files to an archive\n\
315 --concatenate same as -A\n\
316 --delete delete from the archive (not on mag tapes!)\n"),
320 Operation modifiers:\n\
321 -W, --verify attempt to verify the archive after writing it\n\
322 --remove-files remove files after adding them to the archive\n\
323 -k, --keep-old-files don't replace existing files when extracting\n\
324 --overwrite overwrite existing files when extracting\n\
325 -U, --unlink-first remove each file prior to extracting over it\n\
326 --recursive-unlink empty hierarchies prior to extracting directory\n\
327 -S, --sparse handle sparse files efficiently\n\
328 -O, --to-stdout extract files to standard output\n\
329 -G, --incremental handle old GNU-format incremental backup\n\
330 -g, --listed-incremental=FILE\n\
331 handle new GNU-format incremental backup\n\
332 --ignore-failed-read do not exit with nonzero on unreadable files\n"),
336 Handling of file attributes:\n\
337 --owner=NAME force NAME as owner for added files\n\
338 --group=NAME force NAME as group for added files\n\
339 --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
340 --atime-preserve don't change access times on dumped files\n\
341 -m, --modification-time don't extract file modified time\n\
342 --same-owner try extracting files with the same ownership\n\
343 --no-same-owner extract files as yourself\n\
344 --numeric-owner always use numbers for user/group names\n\
345 -p, --same-permissions extract permissions information\n\
346 --no-same-permissions do not extract permissions information\n\
347 --preserve-permissions same as -p\n\
348 -s, --same-order sort names to extract to match archive\n\
349 --preserve-order same as -s\n\
350 --preserve same as both -p and -s\n"),
354 Device selection and switching:\n\
355 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
356 --force-local archive file is local even if has a colon\n\
357 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
358 -[0-7][lmh] specify drive and density\n\
359 -M, --multi-volume create/list/extract multi-volume archive\n\
360 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
361 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
362 --new-volume-script=FILE same as -F FILE\n\
363 --volno-file=FILE use/update the volume number in FILE\n"),
368 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
369 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
370 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
371 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
375 Archive format selection:\n\
376 -V, --label=NAME create archive with volume name NAME\n\
377 PATTERN at list/extract time, a globbing PATTERN\n\
378 -o, --old-archive, --portability write a V7 format archive\n\
379 --posix write a POSIX format archive\n\
380 -j, --bzip2 filter the archive through bzip2\n\
381 -z, --gzip, --ungzip filter the archive through gzip\n\
382 -Z, --compress, --uncompress filter the archive through compress\n\
383 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
387 Local file selection:\n\
388 -C, --directory=DIR change to directory DIR\n\
389 -T, --files-from=NAME get names to extract or create from file NAME\n\
390 --null -T reads null-terminated names, disable -C\n\
391 --exclude=PATTERN exclude files, given as a PATTERN\n\
392 -X, --exclude-from=FILE exclude patterns listed in FILE\n\
393 --anchored exclude patterns match file name start (default)\n\
394 --no-anchored exclude patterns match after any /\n\
395 --ignore-case exclusion ignores case\n\
396 --no-ignore-case exclusion is case sensitive (default)\n\
397 --wildcards exclude patterns use wildcards (default)\n\
398 --no-wildcards exclude patterns are plain strings\n\
399 --wildcards-match-slash exclude pattern wildcards match '/' (default)\n\
400 --no-wildcards-match-slash exclude pattern wildcards do not match '/'\n\
401 -P, --absolute-names don't strip leading `/'s from file names\n\
402 -h, --dereference dump instead the files symlinks point to\n\
403 --no-recursion avoid descending automatically in directories\n\
404 -l, --one-file-system stay in local file system when creating archive\n\
405 -K, --starting-file=NAME begin at file NAME in the archive\n"),
409 -N, --newer=DATE only store files newer than DATE\n\
410 --newer-mtime=DATE compare date and time when data changed only\n\
411 --after-date=DATE same as -N\n"),
415 --backup[=CONTROL] backup before removal, choose version control\n\
416 --suffix=SUFFIX backup before removal, override usual suffix\n"),
420 Informative output:\n\
421 --help print this help, then exit\n\
422 --version print tar program version number, then exit\n\
423 -v, --verbose verbosely list files processed\n\
424 --checkpoint print directory names while reading the archive\n\
425 --totals print total bytes written while creating archive\n\
426 -R, --block-number show block number within archive with each message\n\
427 -w, --interactive ask for confirmation for every action\n\
428 --confirmation same as -w\n"),
432 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
433 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
435 t, numbered make numbered backups\n\
436 nil, existing numbered if numbered backups exist, simple otherwise\n\
437 never, simple always make simple backups\n"),
441 GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
442 is set in the environment, GNU extensions are disallowed with `--posix'.\n\
443 Support for POSIX is only partially implemented, don't count on it yet.\n\
444 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; DATE may be a textual date\n\
445 or a file name starting with `/' or `.', in which case the file's date is used.\n\
446 *This* `tar' defaults to `-f%s -b%d'.\n"),
447 DEFAULT_ARCHIVE
, DEFAULT_BLOCKING
);
448 fputs (_("\nReport bugs to <bug-tar@gnu.org>.\n"), stdout
);
453 /* Parse the options for tar. */
455 /* Available option letters are DEHIJQY and aenqy. Some are reserved:
457 e exit immediately with a nonzero exit status if unexpected errors occur
458 E use extended headers (draft POSIX headers, that is)
459 I same as T (for compatibility with Solaris tar)
460 n the archive is quickly seekable, so don't worry about random seeks
461 q stop after extracting the first occurrence of the named file
462 y per-file gzip compression
463 Y per-block gzip compression */
465 #define OPTION_STRING \
466 "-01234567ABC:F:GIK:L:MN:OPRST:UV:WX:Zb:cdf:g:hijklmoprstuvwxyz"
469 set_subcommand_option (enum subcommand subcommand
)
471 if (subcommand_option
!= UNKNOWN_SUBCOMMAND
472 && subcommand_option
!= subcommand
)
474 _("You may not specify more than one `-Acdtrux' option")));
476 subcommand_option
= subcommand
;
480 set_use_compress_program_option (const char *string
)
482 if (use_compress_program_option
&& strcmp (use_compress_program_option
, string
) != 0)
483 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
485 use_compress_program_option
= string
;
489 decode_options (int argc
, char **argv
)
491 int optchar
; /* option letter */
492 int input_files
; /* number of input files */
493 const char *backup_suffix_string
;
494 const char *version_control_string
= 0;
495 int exclude_options
= EXCLUDE_WILDCARDS
;
497 /* Set some default option values. */
499 subcommand_option
= UNKNOWN_SUBCOMMAND
;
500 archive_format
= DEFAULT_FORMAT
;
501 blocking_factor
= DEFAULT_BLOCKING
;
502 record_size
= DEFAULT_BLOCKING
* BLOCKSIZE
;
503 excluded
= new_exclude ();
504 newer_mtime_option
= TYPE_MINIMUM (time_t);
505 recursion_option
= FNM_LEADING_DIR
;
510 backup_suffix_string
= getenv ("SIMPLE_BACKUP_SUFFIX");
512 /* Convert old-style tar call by exploding option element and rearranging
513 options accordingly. */
515 if (argc
> 1 && argv
[1][0] != '-')
517 int new_argc
; /* argc value for rearranged arguments */
518 char **new_argv
; /* argv value for rearranged arguments */
519 char *const *in
; /* cursor into original argv */
520 char **out
; /* cursor into rearranged argv */
521 const char *letter
; /* cursor into old option letters */
522 char buffer
[3]; /* constructed option buffer */
523 const char *cursor
; /* cursor in OPTION_STRING */
525 /* Initialize a constructed option. */
530 /* Allocate a new argument array, and copy program name in it. */
532 new_argc
= argc
- 1 + strlen (argv
[1]);
533 new_argv
= xmalloc (new_argc
* sizeof (char *));
538 /* Copy each old letter option as a separate option, and have the
539 corresponding argument moved next to it. */
541 for (letter
= *in
++; *letter
; letter
++)
544 *out
++ = xstrdup (buffer
);
545 cursor
= strchr (OPTION_STRING
, *letter
);
546 if (cursor
&& cursor
[1] == ':')
548 if (in
< argv
+ argc
)
551 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
556 /* Copy all remaining options. */
558 while (in
< argv
+ argc
)
561 /* Replace the old option list by the new one. */
567 /* Parse all options and non-options as they appear. */
571 prepend_default_options (getenv ("TAR_OPTIONS"), &argc
, &argv
);
573 while (optchar
= getopt_long (argc
, argv
, OPTION_STRING
, long_options
, 0),
578 usage (TAREXIT_FAILURE
);
584 /* File name or non-parsed option, because of RETURN_IN_ORDER
585 ordering triggered by the leading dash in OPTION_STRING. */
592 set_subcommand_option (CAT_SUBCOMMAND
);
595 case OBSOLETE_BLOCK_COMPRESS
:
596 WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
599 case OBSOLETE_BLOCKING_FACTOR
:
600 WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
606 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
607 && u
== (blocking_factor
= u
)
608 && 0 < blocking_factor
609 && u
== (record_size
= u
* BLOCKSIZE
) / BLOCKSIZE
))
610 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
611 _("Invalid blocking factor")));
615 case OBSOLETE_READ_FULL_RECORDS
:
617 _("Obsolete option name replaced by --read-full-records")));
621 /* Try to reblock input records. For reading 4.2BSD pipes. */
623 /* It would surely make sense to exchange -B and -R, but it seems
624 that -B has been used for a long while in Sun tar ans most
625 BSD-derived systems. This is a consequence of the block/record
626 terminology confusion. */
628 read_full_records_option
= 1;
632 set_subcommand_option (CREATE_SUBCOMMAND
);
641 set_subcommand_option (DIFF_SUBCOMMAND
);
645 if (archive_names
== allocated_archive_names
)
647 allocated_archive_names
*= 2;
649 xrealloc (archive_name_array
,
650 sizeof (const char *) * allocated_archive_names
);
652 archive_name_array
[archive_names
++] = optarg
;
656 /* Since -F is only useful with -M, make it implied. Run this
657 script at the end of each tape. */
659 info_script_option
= optarg
;
660 multi_volume_option
= 1;
664 listed_incremental_option
= optarg
;
665 after_date_option
= 1;
669 /* We are making an incremental dump (FIXME: are we?); save
670 directories at the beginning of the archive, and include in each
671 directory its contents. */
673 incremental_option
= 1;
677 /* Follow symbolic links. */
679 dereference_option
= 1;
683 /* Ignore zero blocks (eofs). This can't be the default,
684 because Unix tar writes two blocks of zeros, then pads out
685 the record with garbage. */
687 ignore_zeros_option
= 1;
692 _("Warning: the -I option is not supported;"
693 " perhaps you meant -j or -T?")));
697 set_use_compress_program_option ("bzip2");
701 /* Don't replace existing files. */
702 old_files_option
= KEEP_OLD_FILES
;
706 starting_file_option
= 1;
711 /* When dumping directories, don't dump files/subdirectories
712 that are on other filesystems. */
714 one_file_system_option
= 1;
720 if (xstrtoumax (optarg
, 0, 10, &u
, "") != LONGINT_OK
)
721 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
722 _("Invalid tape length")));
723 tape_length_option
= 1024 * (tarlong
) u
;
724 multi_volume_option
= 1;
729 WARN ((0, 0, _("Obsolete option name replaced by --touch")));
737 /* Make multivolume archive: when we can't write any more into
738 the archive, re-open it, and continue writing. */
740 multi_volume_option
= 1;
745 after_date_option
= 1;
748 case NEWER_MTIME_OPTION
:
749 if (newer_mtime_option
!= TYPE_MINIMUM (time_t))
750 USAGE_ERROR ((0, 0, _("More than one threshold date")));
752 if (FILESYSTEM_PREFIX_LEN (optarg
) != 0
757 if (deref_stat (dereference_option
, optarg
, &st
) != 0)
760 USAGE_ERROR ((0, 0, _("Date file not found")));
762 newer_mtime_option
= st
.st_mtime
;
766 newer_mtime_option
= get_date (optarg
, 0);
767 if (newer_mtime_option
== (time_t) -1)
768 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
769 tartime (newer_mtime_option
), quote (optarg
)));
773 #endif /* not MSDOS */
776 if (archive_format
== DEFAULT_FORMAT
)
777 archive_format
= V7_FORMAT
;
778 else if (archive_format
!= V7_FORMAT
)
779 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
783 to_stdout_option
= 1;
787 same_permissions_option
= 1;
790 case OBSOLETE_ABSOLUTE_NAMES
:
791 WARN ((0, 0, _("Obsolete option name replaced by --absolute-names")));
795 absolute_names_option
= 1;
799 set_subcommand_option (APPEND_SUBCOMMAND
);
802 case OBSOLETE_BLOCK_NUMBER
:
803 WARN ((0, 0, _("Obsolete option name replaced by --block-number")));
807 /* Print block numbers for debugging bad tar archives. */
809 /* It would surely make sense to exchange -B and -R, but it seems
810 that -B has been used for a long while in Sun tar ans most
811 BSD-derived systems. This is a consequence of the block/record
812 terminology confusion. */
814 block_number_option
= 1;
818 /* Names to extr are sorted. */
820 same_order_option
= 1;
828 set_subcommand_option (LIST_SUBCOMMAND
);
833 files_from_option
= optarg
;
837 set_subcommand_option (UPDATE_SUBCOMMAND
);
841 old_files_option
= UNLINK_FIRST_OLD_FILES
;
849 volume_label_option
= optarg
;
853 interactive_option
= 1;
861 set_subcommand_option (EXTRACT_SUBCOMMAND
);
865 if (add_exclude_file (add_exclude
, excluded
, optarg
,
866 exclude_options
| recursion_option
, '\n')
870 FATAL_ERROR ((0, e
, "%s", quotearg_colon (optarg
)));
876 _("Warning: the -y option is not supported;"
877 " perhaps you meant -j?")));
881 set_use_compress_program_option ("gzip");
885 set_use_compress_program_option ("compress");
888 case OBSOLETE_VERSION_CONTROL
:
889 WARN ((0, 0, _("Obsolete option name replaced by --backup")));
892 case ANCHORED_OPTION
:
893 exclude_options
|= EXCLUDE_ANCHORED
;
899 version_control_string
= optarg
;
903 set_subcommand_option (DELETE_SUBCOMMAND
);
907 add_exclude (excluded
, optarg
, exclude_options
| recursion_option
);
910 case IGNORE_CASE_OPTION
:
911 exclude_options
|= FNM_CASEFOLD
;
915 if (! (strlen (optarg
) < GNAME_FIELD_SIZE
916 && gname_to_gid (optarg
, &group_option
)))
919 if (xstrtoumax (optarg
, 0, 10, &g
, "") == LONGINT_OK
923 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
924 _("%s: Invalid group")));
930 = mode_compile (optarg
,
931 MODE_MASK_EQUALS
| MODE_MASK_PLUS
| MODE_MASK_MINUS
);
932 if (mode_option
== MODE_INVALID
)
933 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
934 if (mode_option
== MODE_MEMORY_EXHAUSTED
)
938 case NO_ANCHORED_OPTION
:
939 exclude_options
&= ~ EXCLUDE_ANCHORED
;
942 case NO_IGNORE_CASE_OPTION
:
943 exclude_options
&= ~ FNM_CASEFOLD
;
946 case NO_WILDCARDS_OPTION
:
947 exclude_options
&= ~ EXCLUDE_WILDCARDS
;
950 case NO_WILDCARDS_MATCH_SLASH_OPTION
:
951 exclude_options
|= FNM_FILE_NAME
;
955 filename_terminator
= '\0';
958 case OVERWRITE_OPTION
:
959 old_files_option
= OVERWRITE_OLD_FILES
;
963 if (! (strlen (optarg
) < UNAME_FIELD_SIZE
964 && uname_to_uid (optarg
, &owner_option
)))
967 if (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
971 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
972 _("Invalid owner")));
977 #if OLDGNU_COMPATIBILITY
978 if (archive_format
== DEFAULT_FORMAT
)
979 archive_format
= GNU_FORMAT
;
980 else if (archive_format
!= GNU_FORMAT
)
981 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
983 if (archive_format
== DEFAULT_FORMAT
)
984 archive_format
= POSIX_FORMAT
;
985 else if (archive_format
!= POSIX_FORMAT
)
986 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
990 case PRESERVE_OPTION
:
991 same_permissions_option
= 1;
992 same_order_option
= 1;
995 case RECORD_SIZE_OPTION
:
998 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
1000 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg
),
1001 _("Invalid record size")));
1003 if (record_size
% BLOCKSIZE
!= 0)
1004 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
1006 blocking_factor
= record_size
/ BLOCKSIZE
;
1010 case RSH_COMMAND_OPTION
:
1011 rsh_command_option
= optarg
;
1016 backup_suffix_string
= optarg
;
1019 case USE_COMPRESS_PROGRAM_OPTION
:
1020 set_use_compress_program_option (optarg
);
1023 case VOLNO_FILE_OPTION
:
1024 volno_file_option
= optarg
;
1027 case WILDCARDS_OPTION
:
1028 exclude_options
|= EXCLUDE_WILDCARDS
;
1031 case WILDCARDS_MATCH_SLASH_OPTION
:
1032 exclude_options
&= ~ FNM_FILE_NAME
;
1044 #ifdef DEVICE_PREFIX
1046 int device
= optchar
- '0';
1048 static char buf
[sizeof DEVICE_PREFIX
+ 10];
1051 density
= getopt_long (argc
, argv
, "lmh", 0, 0);
1052 strcpy (buf
, DEVICE_PREFIX
);
1053 cursor
= buf
+ strlen (buf
);
1055 #ifdef DENSITY_LETTER
1057 sprintf (cursor
, "%d%c", device
, density
);
1059 #else /* not DENSITY_LETTER */
1086 usage (TAREXIT_FAILURE
);
1088 sprintf (cursor
, "%d", device
);
1090 #endif /* not DENSITY_LETTER */
1092 if (archive_names
== allocated_archive_names
)
1094 allocated_archive_names
*= 2;
1095 archive_name_array
=
1096 xrealloc (archive_name_array
,
1097 sizeof (const char *) * allocated_archive_names
);
1099 archive_name_array
[archive_names
++] = buf
;
1101 /* FIXME: How comes this works for many archives when buf is
1106 #else /* not DEVICE_PREFIX */
1109 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1111 #endif /* not DEVICE_PREFIX */
1114 /* Handle operands after any "--" argument. */
1115 for (; optind
< argc
; optind
++)
1117 name_add (argv
[optind
]);
1121 /* Process trivial options. */
1125 printf ("tar (GNU %s) %s\n%s\n%s\n%s\n", PACKAGE
, VERSION
,
1126 "Copyright 2001 Free Software Foundation, Inc.",
1128 This program comes with NO WARRANTY, to the extent permitted by law.\n\
1129 You may redistribute it under the terms of the GNU General Public License;\n\
1130 see the file named COPYING for details."),
1131 _("Written by John Gilmore and Jay Fenlason."));
1132 exit (TAREXIT_SUCCESS
);
1136 usage (TAREXIT_SUCCESS
);
1138 /* Derive option values and check option consistency. */
1140 if (archive_format
== DEFAULT_FORMAT
)
1142 #if OLDGNU_COMPATIBILITY
1143 archive_format
= OLDGNU_FORMAT
;
1145 archive_format
= GNU_FORMAT
;
1149 if (archive_format
== GNU_FORMAT
&& getenv ("POSIXLY_CORRECT"))
1150 archive_format
= POSIX_FORMAT
;
1152 if ((volume_label_option
1153 || incremental_option
|| multi_volume_option
|| sparse_option
)
1154 && archive_format
!= OLDGNU_FORMAT
&& archive_format
!= GNU_FORMAT
)
1156 _("GNU features wanted on incompatible archive format")));
1158 if (archive_names
== 0)
1160 /* If no archive file name given, try TAPE from the environment, or
1161 else, DEFAULT_ARCHIVE from the configuration process. */
1164 archive_name_array
[0] = getenv ("TAPE");
1165 if (! archive_name_array
[0])
1166 archive_name_array
[0] = DEFAULT_ARCHIVE
;
1169 /* Allow multiple archives only with `-M'. */
1171 if (archive_names
> 1 && !multi_volume_option
)
1173 _("Multiple archive files requires `-M' option")));
1175 if (listed_incremental_option
1176 && newer_mtime_option
!= TYPE_MINIMUM (time_t))
1178 _("Cannot combine --listed-incremental with --newer")));
1180 if (volume_label_option
)
1182 size_t volume_label_max_len
=
1183 (sizeof current_header
->header
.name
1184 - 1 /* for trailing '\0' */
1185 - (multi_volume_option
1186 ? (sizeof " Volume "
1187 - 1 /* for null at end of " Volume " */
1188 + INT_STRLEN_BOUND (int) /* for volume number */
1189 - 1 /* for sign, as 0 <= volno */)
1191 if (volume_label_max_len
< strlen (volume_label_option
))
1193 _("%s: Volume label is too long (limit is %lu bytes)"),
1194 quotearg_colon (volume_label_option
),
1195 (unsigned long) volume_label_max_len
));
1198 /* If ready to unlink hierarchies, so we are for simpler files. */
1199 if (recursive_unlink_option
)
1200 old_files_option
= UNLINK_FIRST_OLD_FILES
;
1202 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1203 explicit or implied, is used correctly. */
1205 switch (subcommand_option
)
1207 case CREATE_SUBCOMMAND
:
1208 if (input_files
== 0 && !files_from_option
)
1210 _("Cowardly refusing to create an empty archive")));
1213 case EXTRACT_SUBCOMMAND
:
1214 case LIST_SUBCOMMAND
:
1215 case DIFF_SUBCOMMAND
:
1216 for (archive_name_cursor
= archive_name_array
;
1217 archive_name_cursor
< archive_name_array
+ archive_names
;
1218 archive_name_cursor
++)
1219 if (!strcmp (*archive_name_cursor
, "-"))
1220 request_stdin ("-f");
1223 case CAT_SUBCOMMAND
:
1224 case UPDATE_SUBCOMMAND
:
1225 case APPEND_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
, "-"))
1231 _("Options `-Aru' are incompatible with `-f -'")));
1237 archive_name_cursor
= archive_name_array
;
1239 /* Prepare for generating backup names. */
1241 if (backup_suffix_string
)
1242 simple_backup_suffix
= xstrdup (backup_suffix_string
);
1245 backup_type
= xget_version ("--backup", version_control_string
);
1250 /* Main routine for tar. */
1252 main (int argc
, char **argv
)
1254 #if HAVE_CLOCK_GETTIME
1255 if (clock_gettime (CLOCK_REALTIME
, &start_timespec
) != 0)
1257 start_time
= time (0);
1258 program_name
= argv
[0];
1259 setlocale (LC_ALL
, "");
1260 bindtextdomain (PACKAGE
, LOCALEDIR
);
1261 textdomain (PACKAGE
);
1263 exit_status
= TAREXIT_SUCCESS
;
1264 filename_terminator
= '\n';
1265 set_quoting_style (0, escape_quoting_style
);
1267 /* Pre-allocate a few structures. */
1269 allocated_archive_names
= 10;
1270 archive_name_array
=
1271 xmalloc (sizeof (const char *) * allocated_archive_names
);
1275 /* System V fork+wait does not work if SIGCHLD is ignored. */
1276 signal (SIGCHLD
, SIG_DFL
);
1281 /* Decode options. */
1283 decode_options (argc
, argv
);
1284 name_init (argc
, argv
);
1286 /* Main command execution. */
1288 if (volno_file_option
)
1289 init_volume_number ();
1291 switch (subcommand_option
)
1293 case UNKNOWN_SUBCOMMAND
:
1295 _("You must specify one of the `-Acdtrux' options")));
1297 case CAT_SUBCOMMAND
:
1298 case UPDATE_SUBCOMMAND
:
1299 case APPEND_SUBCOMMAND
:
1303 case DELETE_SUBCOMMAND
:
1304 delete_archive_members ();
1307 case CREATE_SUBCOMMAND
:
1312 print_total_written ();
1315 case EXTRACT_SUBCOMMAND
:
1317 read_and (extract_archive
);
1321 case LIST_SUBCOMMAND
:
1322 read_and (list_archive
);
1325 case DIFF_SUBCOMMAND
:
1327 read_and (diff_archive
);
1331 if (volno_file_option
)
1332 closeout_volume_number ();
1334 /* Dispose of allocated memory, and return. */
1336 free (archive_name_array
);
1339 if (stdlis
== stdout
&& (ferror (stdout
) || fclose (stdout
) != 0))
1340 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1341 if (exit_status
== TAREXIT_FAILURE
)
1342 error (0, 0, _("Error exit delayed from previous errors"));