1 /* A tar (tape archiver) program.
2 Copyright 1988, 92,93,94,95,96,97, 1999 Free Software Foundation, Inc.
3 Written by John Gilmore, starting 1985-08-25.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #if ! defined SIGCHLD && defined SIGCLD
25 # define SIGCHLD SIGCLD
33 /* The following causes "common.h" to produce definitions of all the global
34 variables, rather than just "extern" declarations of them. GNU tar does
35 depend on the system loader to preset all GLOBAL variables to neutral (or
36 zero) values; explicit initialization is usually not done. */
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
PARAMS ((int)) __attribute__ ((noreturn
));
59 /*----------------------------------------------.
60 | Doesn't return if stdin already requested. |
61 `----------------------------------------------*/
63 /* Name of option using stdin. */
64 static const char *stdin_used_by
;
67 request_stdin (const char *option
)
70 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
71 stdin_used_by
, option
));
73 stdin_used_by
= option
;
76 /*--------------------------------------------------------.
77 | Returns true if and only if the user typed 'y' or 'Y'. |
78 `--------------------------------------------------------*/
81 confirm (const char *message_action
, const char *message_name
)
83 static FILE *confirm_file
;
84 static int confirm_file_EOF
;
88 if (archive
== 0 || stdin_used_by
)
90 confirm_file
= fopen (TTY_NAME
, "r");
92 open_fatal (TTY_NAME
);
101 fprintf (stdlis
, "%s %s?", message_action
, quote (message_name
));
105 int reply
= confirm_file_EOF
? EOF
: getc (confirm_file
);
108 for (character
= reply
;
110 character
= getc (confirm_file
))
111 if (character
== EOF
)
113 confirm_file_EOF
= 1;
114 fputc ('\n', stdlis
);
118 return reply
== 'y' || reply
== 'Y';
124 /* For long options that unconditionally set a single flag, we have getopt
125 do it. For the others, we share the code for the equivalent short
126 named option, the name of which is stored in the otherwise-unused `val'
127 field of the `struct option'; for long options that have no equivalent
128 short option, we use non-characters as pseudo short options,
129 starting at CHAR_MAX + 1 and going upwards. */
133 BACKUP_OPTION
= CHAR_MAX
+ 1,
148 USE_COMPRESS_PROGRAM_OPTION
,
151 /* Some cleanup is being made in GNU tar long options. Using old names is
152 allowed for a while, but will also send a warning to stderr. Take old
153 names out in 1.14, or in summer 1997, whichever happens last. */
155 OBSOLETE_ABSOLUTE_NAMES
,
156 OBSOLETE_BLOCK_COMPRESS
,
157 OBSOLETE_BLOCKING_FACTOR
,
158 OBSOLETE_BLOCK_NUMBER
,
159 OBSOLETE_READ_FULL_RECORDS
,
161 OBSOLETE_VERSION_CONTROL
164 /* If nonzero, display usage information and exit. */
165 static int show_help
;
167 /* If nonzero, print the version on standard output and exit. */
168 static int show_version
;
170 struct option long_options
[] =
172 {"absolute-names", no_argument
, 0, 'P'},
173 {"absolute-paths", no_argument
, 0, OBSOLETE_ABSOLUTE_NAMES
},
174 {"after-date", required_argument
, 0, 'N'},
175 {"append", no_argument
, 0, 'r'},
176 {"atime-preserve", no_argument
, &atime_preserve_option
, 1},
177 {"backup", optional_argument
, 0, BACKUP_OPTION
},
178 {"block-compress", no_argument
, 0, OBSOLETE_BLOCK_COMPRESS
},
179 {"block-number", no_argument
, 0, 'R'},
180 {"block-size", required_argument
, 0, OBSOLETE_BLOCKING_FACTOR
},
181 {"blocking-factor", required_argument
, 0, 'b'},
182 {"bzip2", no_argument
, 0, 'I'},
183 {"catenate", no_argument
, 0, 'A'},
184 {"checkpoint", no_argument
, &checkpoint_option
, 1},
185 {"compare", no_argument
, 0, 'd'},
186 {"compress", no_argument
, 0, 'Z'},
187 {"concatenate", no_argument
, 0, 'A'},
188 {"confirmation", no_argument
, 0, 'w'},
189 /* FIXME: --selective as a synonym for --confirmation? */
190 {"create", no_argument
, 0, 'c'},
191 {"delete", no_argument
, 0, DELETE_OPTION
},
192 {"dereference", no_argument
, 0, 'h'},
193 {"diff", no_argument
, 0, 'd'},
194 {"directory", required_argument
, 0, 'C'},
195 {"exclude", required_argument
, 0, EXCLUDE_OPTION
},
196 {"exclude-from", required_argument
, 0, 'X'},
197 {"extract", no_argument
, 0, 'x'},
198 {"file", required_argument
, 0, 'f'},
199 {"files-from", required_argument
, 0, 'T'},
200 {"force-local", no_argument
, &force_local_option
, 1},
201 {"get", no_argument
, 0, 'x'},
202 {"group", required_argument
, 0, GROUP_OPTION
},
203 {"gunzip", no_argument
, 0, 'z'},
204 {"gzip", no_argument
, 0, 'z'},
205 {"help", no_argument
, &show_help
, 1},
206 {"ignore-failed-read", no_argument
, &ignore_failed_read_option
, 1},
207 {"ignore-zeros", no_argument
, 0, 'i'},
208 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
209 {"incremental", no_argument
, 0, 'G'},
210 {"info-script", required_argument
, 0, 'F'},
211 {"interactive", no_argument
, 0, 'w'},
212 {"keep-old-files", no_argument
, 0, 'k'},
213 {"label", required_argument
, 0, 'V'},
214 {"list", no_argument
, 0, 't'},
215 {"listed-incremental", required_argument
, 0, 'g'},
216 {"mode", required_argument
, 0, MODE_OPTION
},
217 {"modification-time", no_argument
, 0, OBSOLETE_TOUCH
},
218 {"multi-volume", no_argument
, 0, 'M'},
219 {"new-volume-script", required_argument
, 0, 'F'},
220 {"newer", required_argument
, 0, 'N'},
221 {"newer-mtime", required_argument
, 0, NEWER_MTIME_OPTION
},
222 {"null", no_argument
, 0, NULL_OPTION
},
223 {"no-recursion", no_argument
, 0, NO_RECURSE_OPTION
},
224 {"no-same-owner", no_argument
, &same_owner_option
, -1},
225 {"no-same-permissions", no_argument
, &same_permissions_option
, -1},
226 {"numeric-owner", no_argument
, &numeric_owner_option
, 1},
227 {"old-archive", no_argument
, 0, 'o'},
228 {"one-file-system", no_argument
, 0, 'l'},
229 {"overwrite", no_argument
, 0, OVERWRITE_OPTION
},
230 {"owner", required_argument
, 0, OWNER_OPTION
},
231 {"portability", no_argument
, 0, 'o'},
232 {"posix", no_argument
, 0, POSIX_OPTION
},
233 {"preserve", no_argument
, 0, PRESERVE_OPTION
},
234 {"preserve-order", no_argument
, 0, 's'},
235 {"preserve-permissions", no_argument
, 0, 'p'},
236 {"recursive-unlink", no_argument
, &recursive_unlink_option
, 1},
237 {"read-full-blocks", no_argument
, 0, OBSOLETE_READ_FULL_RECORDS
},
238 {"read-full-records", no_argument
, 0, 'B'},
239 /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
240 {"record-number", no_argument
, 0, OBSOLETE_BLOCK_NUMBER
},
241 {"record-size", required_argument
, 0, RECORD_SIZE_OPTION
},
242 {"remove-files", no_argument
, &remove_files_option
, 1},
243 {"rsh-command", required_argument
, 0, RSH_COMMAND_OPTION
},
244 {"same-order", no_argument
, 0, 's'},
245 {"same-owner", no_argument
, &same_owner_option
, 1},
246 {"same-permissions", no_argument
, 0, 'p'},
247 {"show-omitted-dirs", no_argument
, &show_omitted_dirs_option
, 1},
248 {"sparse", no_argument
, 0, 'S'},
249 {"starting-file", required_argument
, 0, 'K'},
250 {"suffix", required_argument
, 0, SUFFIX_OPTION
},
251 {"tape-length", required_argument
, 0, 'L'},
252 {"to-stdout", no_argument
, 0, 'O'},
253 {"totals", no_argument
, &totals_option
, 1},
254 {"touch", no_argument
, 0, 'm'},
255 {"uncompress", no_argument
, 0, 'Z'},
256 {"ungzip", no_argument
, 0, 'z'},
257 {"unlink-first", no_argument
, 0, 'U'},
258 {"update", no_argument
, 0, 'u'},
259 {"use-compress-program", required_argument
, 0, USE_COMPRESS_PROGRAM_OPTION
},
260 {"verbose", no_argument
, 0, 'v'},
261 {"verify", no_argument
, 0, 'W'},
262 {"version", no_argument
, &show_version
, 1},
263 {"version-control", required_argument
, 0, OBSOLETE_VERSION_CONTROL
},
264 {"volno-file", required_argument
, 0, VOLNO_FILE_OPTION
},
269 /*---------------------------------------------.
270 | Print a usage message and exit with STATUS. |
271 `---------------------------------------------*/
276 if (status
!= TAREXIT_SUCCESS
)
277 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
282 GNU `tar' saves many files together into a single tape or disk archive, and\n\
283 can restore individual files from the archive.\n"),
285 printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
288 %s -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
289 %s -tvf archive.tar # List all files in archive.tar verbosely.\n\
290 %s -xf archive.tar # Extract all files from archive.tar.\n"),
291 program_name
, program_name
, program_name
, program_name
);
294 If a long option shows an argument as mandatory, then it is mandatory\n\
295 for the equivalent short option also. Similarly for optional arguments.\n"),
299 Main operation mode:\n\
300 -t, --list list the contents of an archive\n\
301 -x, --extract, --get extract files from an archive\n\
302 -c, --create create a new archive\n\
303 -d, --diff, --compare find differences between archive and file system\n\
304 -r, --append append files to the end of an archive\n\
305 -u, --update only append files newer than copy in archive\n\
306 -A, --catenate append tar files to an archive\n\
307 --concatenate same as -A\n\
308 --delete delete from the archive (not on mag tapes!)\n"),
312 Operation modifiers:\n\
313 -W, --verify attempt to verify the archive after writing it\n\
314 --remove-files remove files after adding them to the archive\n\
315 -k, --keep-old-files don't replace existing files when extracting\n\
316 --overwrite overwrite existing files when extracting\n\
317 -U, --unlink-first remove each file prior to extracting over it\n\
318 --recursive-unlink empty hierarchies prior to extracting directory\n\
319 -S, --sparse handle sparse files efficiently\n\
320 -O, --to-stdout extract files to standard output\n\
321 -G, --incremental handle old GNU-format incremental backup\n\
322 -g, --listed-incremental=FILE\n\
323 handle new GNU-format incremental backup\n\
324 --ignore-failed-read do not exit with nonzero on unreadable files\n"),
328 Handling of file attributes:\n\
329 --owner=NAME force NAME as owner for added files\n\
330 --group=NAME force NAME as group for added files\n\
331 --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
332 --atime-preserve don't change access times on dumped files\n\
333 -m, --modification-time don't extract file modified time\n\
334 --same-owner try extracting files with the same ownership\n\
335 --no-same-owner extract files as yourself\n\
336 --numeric-owner always use numbers for user/group names\n\
337 -p, --same-permissions extract permissions information\n\
338 --no-same-permissions do not extract permissions information\n\
339 --preserve-permissions same as -p\n\
340 -s, --same-order sort names to extract to match archive\n\
341 --preserve-order same as -s\n\
342 --preserve same as both -p and -s\n"),
346 Device selection and switching:\n\
347 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
348 --force-local archive file is local even if has a colon\n\
349 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
350 -[0-7][lmh] specify drive and density\n\
351 -M, --multi-volume create/list/extract multi-volume archive\n\
352 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
353 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
354 --new-volume-script=FILE same as -F FILE\n\
355 --volno-file=FILE use/update the volume number in FILE\n"),
360 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
361 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
362 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
363 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
367 Archive format selection:\n\
368 -V, --label=NAME create archive with volume name NAME\n\
369 PATTERN at list/extract time, a globbing PATTERN\n\
370 -o, --old-archive, --portability write a V7 format archive\n\
371 --posix write a POSIX format archive\n\
372 -I, --bzip2 filter the archive through bzip2\n\
373 -z, --gzip, --ungzip filter the archive through gzip\n\
374 -Z, --compress, --uncompress filter the archive through compress\n\
375 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
379 Local file selection:\n\
380 -C, --directory=DIR change to directory DIR\n\
381 -T, --files-from=NAME get names to extract or create from file NAME\n\
382 --null -T reads null-terminated names, disable -C\n\
383 --exclude=PATTERN exclude files, given as a globbing PATTERN\n\
384 -X, --exclude-from=FILE exclude globbing patterns listed in FILE\n\
385 -P, --absolute-names don't strip leading `/'s from file names\n\
386 -h, --dereference dump instead the files symlinks point to\n\
387 --no-recursion avoid descending automatically in directories\n\
388 -l, --one-file-system stay in local file system when creating archive\n\
389 -K, --starting-file=NAME begin at file NAME in the archive\n"),
393 -N, --newer=DATE only store files newer than DATE\n\
394 --newer-mtime compare date and time when data changed only\n\
395 --after-date=DATE same as -N\n"),
399 --backup[=CONTROL] backup before removal, choose version control\n\
400 --suffix=SUFFIX backup before removal, override usual suffix\n"),
404 Informative output:\n\
405 --help print this help, then exit\n\
406 --version print tar program version number, then exit\n\
407 -v, --verbose verbosely list files processed\n\
408 --checkpoint print directory names while reading the archive\n\
409 --totals print total bytes written while creating archive\n\
410 -R, --block-number show block number within archive with each message\n\
411 -w, --interactive ask for confirmation for every action\n\
412 --confirmation same as -w\n"),
416 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
417 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
419 t, numbered make numbered backups\n\
420 nil, existing numbered if numbered backups exist, simple otherwise\n\
421 never, simple always make simple backups\n"),
425 GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
426 is set in the environment, GNU extensions are disallowed with `--posix'.\n\
427 Support for POSIX is only partially implemented, don't count on it yet.\n\
428 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; and FILE may be a file\n\
429 or a device. *This* `tar' defaults to `-f%s -b%d'.\n"),
430 DEFAULT_ARCHIVE
, DEFAULT_BLOCKING
);
431 fputs (_("\nReport bugs to <bug-tar@gnu.org>.\n"), stdout
);
436 /*----------------------------.
437 | Parse the options for tar. |
438 `----------------------------*/
440 /* Available option letters are DEHJQY and aejnqy. Some are reserved:
442 y per-file gzip compression
443 Y per-block gzip compression */
445 #define OPTION_STRING \
446 "-01234567ABC:F:GIK:L:MN:OPRST:UV:WX:Zb:cdf:g:hiklmoprstuvwxz"
449 set_subcommand_option (enum subcommand subcommand
)
451 if (subcommand_option
!= UNKNOWN_SUBCOMMAND
452 && subcommand_option
!= subcommand
)
454 _("You may not specify more than one `-Acdtrux' option")));
456 subcommand_option
= subcommand
;
460 set_use_compress_program_option (const char *string
)
462 if (use_compress_program_option
&& strcmp (use_compress_program_option
, string
) != 0)
463 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
465 use_compress_program_option
= string
;
468 /* Ignore DUMMY (which will always be null in practice), and add
469 PATTERN to the proper set of patterns to be excluded -- either
470 patterns with slashes, or patterns without. */
472 add_filtered_exclude (struct exclude
*dummy
, char const *pattern
)
474 add_exclude ((strchr (pattern
, '/')
475 ? excluded_with_slash
476 : excluded_without_slash
),
481 decode_options (int argc
, char *const *argv
)
483 int optchar
; /* option letter */
484 int input_files
; /* number of input files */
485 const char *backup_suffix_string
;
486 const char *version_control_string
= 0;
488 /* Set some default option values. */
490 subcommand_option
= UNKNOWN_SUBCOMMAND
;
491 archive_format
= DEFAULT_FORMAT
;
492 blocking_factor
= DEFAULT_BLOCKING
;
493 record_size
= DEFAULT_BLOCKING
* BLOCKSIZE
;
494 excluded_with_slash
= new_exclude ();
495 excluded_without_slash
= new_exclude ();
496 newer_mtime_option
= TYPE_MINIMUM (time_t);
501 backup_suffix_string
= getenv ("SIMPLE_BACKUP_SUFFIX");
503 /* Convert old-style tar call by exploding option element and rearranging
504 options accordingly. */
506 if (argc
> 1 && argv
[1][0] != '-')
508 int new_argc
; /* argc value for rearranged arguments */
509 char **new_argv
; /* argv value for rearranged arguments */
510 char *const *in
; /* cursor into original argv */
511 char **out
; /* cursor into rearranged argv */
512 const char *letter
; /* cursor into old option letters */
513 char buffer
[3]; /* constructed option buffer */
514 const char *cursor
; /* cursor in OPTION_STRING */
516 /* Initialize a constructed option. */
521 /* Allocate a new argument array, and copy program name in it. */
523 new_argc
= argc
- 1 + strlen (argv
[1]);
524 new_argv
= xmalloc (new_argc
* sizeof (char *));
529 /* Copy each old letter option as a separate option, and have the
530 corresponding argument moved next to it. */
532 for (letter
= *in
++; *letter
; letter
++)
535 *out
++ = xstrdup (buffer
);
536 cursor
= strchr (OPTION_STRING
, *letter
);
537 if (cursor
&& cursor
[1] == ':')
539 if (in
< argv
+ argc
)
542 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
547 /* Copy all remaining options. */
549 while (in
< argv
+ argc
)
552 /* Replace the old option list by the new one. */
558 /* Parse all options and non-options as they appear. */
562 while (optchar
= getopt_long (argc
, argv
, OPTION_STRING
, long_options
, 0),
567 usage (TAREXIT_FAILURE
);
573 /* File name or non-parsed option, because of RETURN_IN_ORDER
574 ordering triggered by the leading dash in OPTION_STRING. */
581 set_subcommand_option (CAT_SUBCOMMAND
);
584 case OBSOLETE_BLOCK_COMPRESS
:
585 WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
588 case OBSOLETE_BLOCKING_FACTOR
:
589 WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
595 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
596 && u
== (blocking_factor
= u
)
597 && 0 < blocking_factor
598 && u
== (record_size
= u
* BLOCKSIZE
) / BLOCKSIZE
))
599 USAGE_ERROR ((0, 0, _("Invalid blocking factor")));
603 case OBSOLETE_READ_FULL_RECORDS
:
605 _("Obsolete option name replaced by --read-full-records")));
609 /* Try to reblock input records. For reading 4.2BSD pipes. */
611 /* It would surely make sense to exchange -B and -R, but it seems
612 that -B has been used for a long while in Sun tar ans most
613 BSD-derived systems. This is a consequence of the block/record
614 terminology confusion. */
616 read_full_records_option
= 1;
620 set_subcommand_option (CREATE_SUBCOMMAND
);
629 set_subcommand_option (DIFF_SUBCOMMAND
);
633 if (archive_names
== allocated_archive_names
)
635 allocated_archive_names
*= 2;
637 xrealloc (archive_name_array
,
638 sizeof (const char *) * allocated_archive_names
);
640 archive_name_array
[archive_names
++] = optarg
;
644 /* Since -F is only useful with -M, make it implied. Run this
645 script at the end of each tape. */
647 info_script_option
= optarg
;
648 multi_volume_option
= 1;
652 listed_incremental_option
= optarg
;
653 after_date_option
= 1;
657 /* We are making an incremental dump (FIXME: are we?); save
658 directories at the beginning of the archive, and include in each
659 directory its contents. */
661 incremental_option
= 1;
665 /* Follow symbolic links. */
667 dereference_option
= 1;
671 /* Ignore zero blocks (eofs). This can't be the default,
672 because Unix tar writes two blocks of zeros, then pads out
673 the record with garbage. */
675 ignore_zeros_option
= 1;
679 set_use_compress_program_option ("bzip2");
683 /* Don't replace existing files. */
684 old_files_option
= KEEP_OLD_FILES
;
688 starting_file_option
= 1;
693 /* When dumping directories, don't dump files/subdirectories
694 that are on other filesystems. */
696 one_file_system_option
= 1;
702 if (xstrtoumax (optarg
, 0, 10, &u
, "") != LONGINT_OK
)
703 USAGE_ERROR ((0, 0, _("Invalid tape length")));
704 tape_length_option
= 1024 * (tarlong
) u
;
705 multi_volume_option
= 1;
710 WARN ((0, 0, _("Obsolete option name replaced by --touch")));
718 /* Make multivolume archive: when we can't write any more into
719 the archive, re-open it, and continue writing. */
721 multi_volume_option
= 1;
726 after_date_option
= 1;
729 case NEWER_MTIME_OPTION
:
730 if (newer_mtime_option
!= TYPE_MINIMUM (time_t))
731 USAGE_ERROR ((0, 0, _("More than one threshold date")));
733 newer_mtime_option
= get_date (optarg
, 0);
734 if (newer_mtime_option
== (time_t) -1)
735 WARN ((0, 0, _("Substituting %s for unknown date format %s"),
736 tartime (newer_mtime_option
), quote (optarg
)));
739 #endif /* not MSDOS */
742 if (archive_format
== DEFAULT_FORMAT
)
743 archive_format
= V7_FORMAT
;
744 else if (archive_format
!= V7_FORMAT
)
745 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
749 to_stdout_option
= 1;
753 same_permissions_option
= 1;
756 case OBSOLETE_ABSOLUTE_NAMES
:
757 WARN ((0, 0, _("Obsolete option name replaced by --absolute-names")));
761 absolute_names_option
= 1;
765 set_subcommand_option (APPEND_SUBCOMMAND
);
768 case OBSOLETE_BLOCK_NUMBER
:
769 WARN ((0, 0, _("Obsolete option name replaced by --block-number")));
773 /* Print block numbers for debugging bad tar archives. */
775 /* It would surely make sense to exchange -B and -R, but it seems
776 that -B has been used for a long while in Sun tar ans most
777 BSD-derived systems. This is a consequence of the block/record
778 terminology confusion. */
780 block_number_option
= 1;
784 /* Names to extr are sorted. */
786 same_order_option
= 1;
794 set_subcommand_option (LIST_SUBCOMMAND
);
799 files_from_option
= optarg
;
803 set_subcommand_option (UPDATE_SUBCOMMAND
);
807 old_files_option
= UNLINK_FIRST_OLD_FILES
;
815 volume_label_option
= optarg
;
819 interactive_option
= 1;
827 set_subcommand_option (EXTRACT_SUBCOMMAND
);
831 if (add_exclude_file (add_filtered_exclude
, 0, optarg
, '\n') != 0)
834 FATAL_ERROR ((0, e
, "%s", quotearg_colon (optarg
)));
839 set_use_compress_program_option ("gzip");
843 set_use_compress_program_option ("compress");
846 case OBSOLETE_VERSION_CONTROL
:
847 WARN ((0, 0, _("Obsolete option name replaced by --backup")));
853 version_control_string
= optarg
;
857 set_subcommand_option (DELETE_SUBCOMMAND
);
861 add_filtered_exclude (0, optarg
);
865 if (! (strlen (optarg
) < GNAME_FIELD_SIZE
866 && gname_to_gid (optarg
, &group_option
)))
869 if (xstrtoumax (optarg
, 0, 10, &g
, "") == LONGINT_OK
873 FATAL_ERROR ((0, 0, _("Invalid group given on option")));
879 = mode_compile (optarg
,
880 MODE_MASK_EQUALS
| MODE_MASK_PLUS
| MODE_MASK_MINUS
);
881 if (mode_option
== MODE_INVALID
)
882 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
883 if (mode_option
== MODE_MEMORY_EXHAUSTED
)
884 FATAL_ERROR ((0, 0, _("Memory exhausted")));
887 case NO_RECURSE_OPTION
:
888 no_recurse_option
= 1;
892 filename_terminator
= '\0';
895 case OVERWRITE_OPTION
:
896 old_files_option
= OVERWRITE_OLD_FILES
;
900 if (! (strlen (optarg
) < UNAME_FIELD_SIZE
901 && uname_to_uid (optarg
, &owner_option
)))
904 if (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
908 FATAL_ERROR ((0, 0, _("Invalid owner given on option")));
913 #if OLDGNU_COMPATIBILITY
914 if (archive_format
== DEFAULT_FORMAT
)
915 archive_format
= GNU_FORMAT
;
916 else if (archive_format
!= GNU_FORMAT
)
917 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
919 if (archive_format
== DEFAULT_FORMAT
)
920 archive_format
= POSIX_FORMAT
;
921 else if (archive_format
!= POSIX_FORMAT
)
922 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
926 case PRESERVE_OPTION
:
927 same_permissions_option
= 1;
928 same_order_option
= 1;
931 case RECORD_SIZE_OPTION
:
934 if (! (xstrtoumax (optarg
, 0, 10, &u
, "") == LONGINT_OK
936 USAGE_ERROR ((0, 0, _("Invalid record size")));
938 if (record_size
% BLOCKSIZE
!= 0)
939 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
941 blocking_factor
= record_size
/ BLOCKSIZE
;
945 case RSH_COMMAND_OPTION
:
946 rsh_command_option
= optarg
;
951 backup_suffix_string
= optarg
;
954 case VOLNO_FILE_OPTION
:
955 volno_file_option
= optarg
;
958 case USE_COMPRESS_PROGRAM_OPTION
:
959 set_use_compress_program_option (optarg
);
973 int device
= optchar
- '0';
975 static char buf
[sizeof DEVICE_PREFIX
+ 10];
978 density
= getopt_long (argc
, argv
, "lmh", 0, 0);
979 strcpy (buf
, DEVICE_PREFIX
);
980 cursor
= buf
+ strlen (buf
);
982 #ifdef DENSITY_LETTER
984 sprintf (cursor
, "%d%c", device
, density
);
986 #else /* not DENSITY_LETTER */
1013 usage (TAREXIT_FAILURE
);
1015 sprintf (cursor
, "%d", device
);
1017 #endif /* not DENSITY_LETTER */
1019 if (archive_names
== allocated_archive_names
)
1021 allocated_archive_names
*= 2;
1022 archive_name_array
=
1023 xrealloc (archive_name_array
,
1024 sizeof (const char *) * allocated_archive_names
);
1026 archive_name_array
[archive_names
++] = buf
;
1028 /* FIXME: How comes this works for many archives when buf is
1033 #else /* not DEVICE_PREFIX */
1036 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1038 #endif /* not DEVICE_PREFIX */
1041 /* Handle operands after any "--" argument. */
1042 for (; optind
< argc
; optind
++)
1044 name_add (argv
[optind
]);
1048 /* Process trivial options. */
1052 printf ("tar (GNU %s) %s\n%s\n%s\n%s\n", PACKAGE
, VERSION
,
1053 "Copyright 1999 Free Software Foundation, Inc.",
1055 This program comes with NO WARRANTY, to the extent permitted by law.\n\
1056 You may redistribute it under the terms of the GNU General Public License;\n\
1057 see the file named COPYING for details."),
1058 _("Written by John Gilmore and Jay Fenlason."));
1059 exit (TAREXIT_SUCCESS
);
1063 usage (TAREXIT_SUCCESS
);
1065 /* Derive option values and check option consistency. */
1067 if (archive_format
== DEFAULT_FORMAT
)
1069 #if OLDGNU_COMPATIBILITY
1070 archive_format
= OLDGNU_FORMAT
;
1072 archive_format
= GNU_FORMAT
;
1076 if (archive_format
== GNU_FORMAT
&& getenv ("POSIXLY_CORRECT"))
1077 archive_format
= POSIX_FORMAT
;
1079 if ((volume_label_option
1080 || incremental_option
|| multi_volume_option
|| sparse_option
)
1081 && archive_format
!= OLDGNU_FORMAT
&& archive_format
!= GNU_FORMAT
)
1083 _("GNU features wanted on incompatible archive format")));
1085 if (archive_names
== 0)
1087 /* If no archive file name given, try TAPE from the environment, or
1088 else, DEFAULT_ARCHIVE from the configuration process. */
1091 archive_name_array
[0] = getenv ("TAPE");
1092 if (! archive_name_array
[0])
1093 archive_name_array
[0] = DEFAULT_ARCHIVE
;
1096 /* Allow multiple archives only with `-M'. */
1098 if (archive_names
> 1 && !multi_volume_option
)
1100 _("Multiple archive files requires `-M' option")));
1102 if (listed_incremental_option
1103 && newer_mtime_option
!= TYPE_MINIMUM (time_t))
1105 _("Cannot combine --listed-incremental with --newer")));
1107 /* If ready to unlink hierarchies, so we are for simpler files. */
1108 if (recursive_unlink_option
)
1109 old_files_option
= UNLINK_FIRST_OLD_FILES
;
1111 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1112 explicit or implied, is used correctly. */
1114 switch (subcommand_option
)
1116 case CREATE_SUBCOMMAND
:
1117 if (input_files
== 0 && !files_from_option
)
1119 _("Cowardly refusing to create an empty archive")));
1122 case EXTRACT_SUBCOMMAND
:
1123 case LIST_SUBCOMMAND
:
1124 case DIFF_SUBCOMMAND
:
1125 for (archive_name_cursor
= archive_name_array
;
1126 archive_name_cursor
< archive_name_array
+ archive_names
;
1127 archive_name_cursor
++)
1128 if (!strcmp (*archive_name_cursor
, "-"))
1129 request_stdin ("-f");
1132 case CAT_SUBCOMMAND
:
1133 case UPDATE_SUBCOMMAND
:
1134 case APPEND_SUBCOMMAND
:
1135 for (archive_name_cursor
= archive_name_array
;
1136 archive_name_cursor
< archive_name_array
+ archive_names
;
1137 archive_name_cursor
++)
1138 if (!strcmp (*archive_name_cursor
, "-"))
1140 _("Options `-Aru' are incompatible with `-f -'")));
1146 archive_name_cursor
= archive_name_array
;
1148 /* Prepare for generating backup names. */
1150 if (backup_suffix_string
)
1151 simple_backup_suffix
= xstrdup (backup_suffix_string
);
1154 backup_type
= xget_version ("--backup", version_control_string
);
1159 /*-----------------------.
1160 | Main routine for tar. |
1161 `-----------------------*/
1164 main (int argc
, char *const *argv
)
1166 #if HAVE_CLOCK_GETTIME
1167 if (clock_gettime (CLOCK_REALTIME
, &start_timespec
) != 0)
1169 start_time
= time (0);
1170 program_name
= argv
[0];
1171 setlocale (LC_ALL
, "");
1172 bindtextdomain (PACKAGE
, LOCALEDIR
);
1173 textdomain (PACKAGE
);
1175 exit_status
= TAREXIT_SUCCESS
;
1176 filename_terminator
= '\n';
1177 set_quoting_style (0, escape_quoting_style
);
1179 /* Pre-allocate a few structures. */
1181 allocated_archive_names
= 10;
1182 archive_name_array
=
1183 xmalloc (sizeof (const char *) * allocated_archive_names
);
1187 /* System V fork+wait does not work if SIGCHLD is ignored. */
1188 signal (SIGCHLD
, SIG_DFL
);
1193 /* Decode options. */
1195 decode_options (argc
, argv
);
1196 name_init (argc
, argv
);
1198 /* Main command execution. */
1200 if (volno_file_option
)
1201 init_volume_number ();
1203 switch (subcommand_option
)
1205 case UNKNOWN_SUBCOMMAND
:
1207 _("You must specify one of the `-Acdtrux' options")));
1209 case CAT_SUBCOMMAND
:
1210 case UPDATE_SUBCOMMAND
:
1211 case APPEND_SUBCOMMAND
:
1215 case DELETE_SUBCOMMAND
:
1216 delete_archive_members ();
1219 case CREATE_SUBCOMMAND
:
1224 print_total_written ();
1227 case EXTRACT_SUBCOMMAND
:
1229 read_and (extract_archive
);
1232 case LIST_SUBCOMMAND
:
1233 read_and (list_archive
);
1236 case DIFF_SUBCOMMAND
:
1238 read_and (diff_archive
);
1242 if (volno_file_option
)
1243 closeout_volume_number ();
1245 /* Dispose of allocated memory, and return. */
1247 free (archive_name_array
);
1250 if (stdlis
== stdout
&& (ferror (stdout
) || fclose (stdout
) != 0))
1251 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1252 if (exit_status
== TAREXIT_FAILURE
)
1253 error (0, 0, _("Error exit delayed from previous errors"));