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
28 /* The following causes "common.h" to produce definitions of all the global
29 variables, rather than just "extern" declarations of them. GNU tar does
30 depend on the system loader to preset all GLOBAL variables to neutral (or
31 zero) values, explicit initialisation is usually not done. */
39 /* Local declarations. */
41 #ifndef DEFAULT_ARCHIVE
42 # define DEFAULT_ARCHIVE "tar.out"
45 #ifndef DEFAULT_BLOCKING
46 # define DEFAULT_BLOCKING 20
49 static void usage
PARAMS ((int));
53 /*----------------------------------------------.
54 | Doesn't return if stdin already requested. |
55 `----------------------------------------------*/
57 /* Name of option using stdin. */
58 static const char *stdin_used_by
= NULL
;
61 request_stdin (const char *option
)
64 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
65 stdin_used_by
, option
));
67 stdin_used_by
= option
;
70 /*--------------------------------------------------------.
71 | Returns true if and only if the user typed 'y' or 'Y'. |
72 `--------------------------------------------------------*/
75 confirm (const char *message_action
, const char *message_name
)
77 static FILE *confirm_file
= NULL
;
81 if (archive
== 0 || stdin_used_by
)
82 confirm_file
= fopen (TTY_NAME
, "r");
90 FATAL_ERROR ((0, 0, _("Cannot read confirmation from user")));
93 fprintf (stdlis
, "%s %s?", message_action
, message_name
);
97 int reply
= getc (confirm_file
);
100 for (character
= reply
;
101 character
!= '\n' && character
!= EOF
;
102 character
= getc (confirm_file
))
104 return reply
== 'y' || reply
== 'Y';
110 /* For long options that unconditionally set a single flag, we have getopt
111 do it. For the others, we share the code for the equivalent short
112 named option, the name of which is stored in the otherwise-unused `val'
113 field of the `struct option'; for long options that have no equivalent
114 short option, we use non-characters as pseudo short options,
115 starting at CHAR_MAX + 1 and going upwards. */
119 BACKUP_OPTION
= CHAR_MAX
+ 1,
133 USE_COMPRESS_PROGRAM_OPTION
,
136 /* Some cleanup is being made in GNU tar long options. Using old names is
137 allowed for a while, but will also send a warning to stderr. Take old
138 names out in 1.14, or in summer 1997, whichever happens last. */
140 OBSOLETE_ABSOLUTE_NAMES
,
141 OBSOLETE_BLOCK_COMPRESS
,
142 OBSOLETE_BLOCKING_FACTOR
,
143 OBSOLETE_BLOCK_NUMBER
,
144 OBSOLETE_READ_FULL_RECORDS
,
146 OBSOLETE_VERSION_CONTROL
149 /* If nonzero, display usage information and exit. */
150 static int show_help
= 0;
152 /* If nonzero, print the version on standard output and exit. */
153 static int show_version
= 0;
155 struct option long_options
[] =
157 {"absolute-names", no_argument
, NULL
, 'P'},
158 {"absolute-paths", no_argument
, NULL
, OBSOLETE_ABSOLUTE_NAMES
},
159 {"after-date", required_argument
, NULL
, 'N'},
160 {"append", no_argument
, NULL
, 'r'},
161 {"atime-preserve", no_argument
, &atime_preserve_option
, 1},
162 {"backup", optional_argument
, NULL
, BACKUP_OPTION
},
163 {"block-compress", no_argument
, NULL
, OBSOLETE_BLOCK_COMPRESS
},
164 {"block-number", no_argument
, NULL
, 'R'},
165 {"block-size", required_argument
, NULL
, OBSOLETE_BLOCKING_FACTOR
},
166 {"blocking-factor", required_argument
, NULL
, 'b'},
167 {"bzip2", no_argument
, NULL
, 'y'},
168 {"catenate", no_argument
, NULL
, 'A'},
169 {"checkpoint", no_argument
, &checkpoint_option
, 1},
170 {"compare", no_argument
, NULL
, 'd'},
171 {"compress", no_argument
, NULL
, 'Z'},
172 {"concatenate", no_argument
, NULL
, 'A'},
173 {"confirmation", no_argument
, NULL
, 'w'},
174 /* FIXME: --selective as a synonym for --confirmation? */
175 {"create", no_argument
, NULL
, 'c'},
176 {"delete", no_argument
, NULL
, DELETE_OPTION
},
177 {"dereference", no_argument
, NULL
, 'h'},
178 {"diff", no_argument
, NULL
, 'd'},
179 {"directory", required_argument
, NULL
, 'C'},
180 {"exclude", required_argument
, NULL
, EXCLUDE_OPTION
},
181 {"exclude-from", required_argument
, NULL
, 'X'},
182 {"extract", no_argument
, NULL
, 'x'},
183 {"file", required_argument
, NULL
, 'f'},
184 {"files-from", required_argument
, NULL
, 'T'},
185 {"force-local", no_argument
, &force_local_option
, 1},
186 {"get", no_argument
, NULL
, 'x'},
187 {"group", required_argument
, NULL
, GROUP_OPTION
},
188 {"gunzip", no_argument
, NULL
, 'z'},
189 {"gzip", no_argument
, NULL
, 'z'},
190 {"help", no_argument
, &show_help
, 1},
191 {"ignore-failed-read", no_argument
, &ignore_failed_read_option
, 1},
192 {"ignore-zeros", no_argument
, NULL
, 'i'},
193 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
194 {"incremental", no_argument
, NULL
, 'G'},
195 {"info-script", required_argument
, NULL
, 'F'},
196 {"interactive", no_argument
, NULL
, 'w'},
197 {"keep-old-files", no_argument
, NULL
, 'k'},
198 {"label", required_argument
, NULL
, 'V'},
199 {"list", no_argument
, NULL
, 't'},
200 {"listed-incremental", required_argument
, NULL
, 'g'},
201 {"mode", required_argument
, NULL
, MODE_OPTION
},
202 {"modification-time", no_argument
, NULL
, OBSOLETE_TOUCH
},
203 {"multi-volume", no_argument
, NULL
, 'M'},
204 {"new-volume-script", required_argument
, NULL
, 'F'},
205 {"newer", required_argument
, NULL
, 'N'},
206 {"newer-mtime", required_argument
, NULL
, NEWER_MTIME_OPTION
},
207 {"null", no_argument
, NULL
, NULL_OPTION
},
208 {"no-recursion", no_argument
, NULL
, NO_RECURSE_OPTION
},
209 {"numeric-owner", no_argument
, &numeric_owner_option
, 1},
210 {"old-archive", no_argument
, NULL
, 'o'},
211 {"one-file-system", no_argument
, NULL
, 'l'},
212 {"owner", required_argument
, NULL
, OWNER_OPTION
},
213 {"portability", no_argument
, NULL
, 'o'},
214 {"posix", no_argument
, NULL
, POSIX_OPTION
},
215 {"preserve", no_argument
, NULL
, PRESERVE_OPTION
},
216 {"preserve-order", no_argument
, NULL
, 's'},
217 {"preserve-permissions", no_argument
, NULL
, 'p'},
218 {"recursive-unlink", no_argument
, &recursive_unlink_option
, 1},
219 {"read-full-blocks", no_argument
, NULL
, OBSOLETE_READ_FULL_RECORDS
},
220 {"read-full-records", no_argument
, NULL
, 'B'},
221 /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
222 {"record-number", no_argument
, NULL
, OBSOLETE_BLOCK_NUMBER
},
223 {"record-size", required_argument
, NULL
, RECORD_SIZE_OPTION
},
224 {"remove-files", no_argument
, &remove_files_option
, 1},
225 {"rsh-command", required_argument
, NULL
, RSH_COMMAND_OPTION
},
226 {"same-order", no_argument
, NULL
, 's'},
227 {"same-owner", no_argument
, &same_owner_option
, 1},
228 {"same-permissions", no_argument
, NULL
, 'p'},
229 {"show-omitted-dirs", no_argument
, &show_omitted_dirs_option
, 1},
230 {"sparse", no_argument
, NULL
, 'S'},
231 {"starting-file", required_argument
, NULL
, 'K'},
232 {"suffix", required_argument
, NULL
, SUFFIX_OPTION
},
233 {"tape-length", required_argument
, NULL
, 'L'},
234 {"to-stdout", no_argument
, NULL
, 'O'},
235 {"totals", no_argument
, &totals_option
, 1},
236 {"touch", no_argument
, NULL
, 'm'},
237 {"uncompress", no_argument
, NULL
, 'Z'},
238 {"ungzip", no_argument
, NULL
, 'z'},
239 {"unlink-first", no_argument
, NULL
, 'U'},
240 {"update", no_argument
, NULL
, 'u'},
241 {"use-compress-program", required_argument
, NULL
, USE_COMPRESS_PROGRAM_OPTION
},
242 {"verbose", no_argument
, NULL
, 'v'},
243 {"verify", no_argument
, NULL
, 'W'},
244 {"version", no_argument
, &show_version
, 1},
245 {"version-control", required_argument
, NULL
, OBSOLETE_VERSION_CONTROL
},
246 {"volno-file", required_argument
, NULL
, VOLNO_FILE_OPTION
},
251 /*---------------------------------------------.
252 | Print a usage message and exit with STATUS. |
253 `---------------------------------------------*/
258 if (status
!= TAREXIT_SUCCESS
)
259 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
264 GNU `tar' saves many files together into a single tape or disk archive, and\n\
265 can restore individual files from the archive.\n"),
267 printf (_("\nUsage: %s [OPTION]... [FILE]...\n"), program_name
);
270 If a long option shows an argument as mandatory, then it is mandatory\n\
271 for the equivalent short option also. Similarly for optional arguments.\n"),
275 Main operation mode:\n\
276 -t, --list list the contents of an archive\n\
277 -x, --extract, --get extract files from an archive\n\
278 -c, --create create a new archive\n\
279 -d, --diff, --compare find differences between archive and file system\n\
280 -r, --append append files to the end of an archive\n\
281 -u, --update only append files newer than copy in archive\n\
282 -A, --catenate append tar files to an archive\n\
283 --concatenate same as -A\n\
284 --delete delete from the archive (not on mag tapes!)\n"),
288 Operation modifiers:\n\
289 -W, --verify attempt to verify the archive after writing it\n\
290 --remove-files remove files after adding them to the archive\n\
291 -k, --keep-old-files don't overwrite existing files when extracting\n\
292 -U, --unlink-first remove each file prior to extracting over it\n\
293 --recursive-unlink empty hierarchies prior to extracting directory\n\
294 -S, --sparse handle sparse files efficiently\n\
295 -O, --to-stdout extract files to standard output\n\
296 -G, --incremental handle old GNU-format incremental backup\n\
297 -g, --listed-incremental handle new GNU-format incremental backup\n\
298 --ignore-failed-read do not exit with nonzero on unreadable files\n"),
302 Handling of file attributes:\n\
303 --owner=NAME force NAME as owner for added files\n\
304 --group=NAME force NAME as group for added files\n\
305 --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
306 --atime-preserve don't change access times on dumped files\n\
307 -m, --modification-time don't extract file modified time\n\
308 --same-owner try extracting files with the same ownership\n\
309 --numeric-owner always use numbers for user/group names\n\
310 -p, --same-permissions extract all protection information\n\
311 --preserve-permissions same as -p\n\
312 -s, --same-order sort names to extract to match archive\n\
313 --preserve-order same as -s\n\
314 --preserve same as both -p and -s\n"),
318 Device selection and switching:\n\
319 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
320 --force-local archive file is local even if has a colon\n\
321 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
322 -[0-7][lmh] specify drive and density\n\
323 -M, --multi-volume create/list/extract multi-volume archive\n\
324 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
325 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
326 --new-volume-script=FILE same as -F FILE\n\
327 --volno-file=FILE use/update the volume number in FILE\n"),
332 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
333 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
334 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
335 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
339 Archive format selection:\n\
340 -V, --label=NAME create archive with volume name NAME\n\
341 PATTERN at list/extract time, a globbing PATTERN\n\
342 -o, --old-archive, --portability write a V7 format archive\n\
343 --posix write a POSIX conformant archive\n\
344 -y, --bzip2 filter the archive through bzip2\n\
345 -z, --gzip, --ungzip filter the archive through gzip\n\
346 -Z, --compress, --uncompress filter the archive through compress\n\
347 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
351 Local file selection:\n\
352 -C, --directory=DIR change to directory DIR\n\
353 -T, --files-from=NAME get names to extract or create from file NAME\n\
354 --null -T reads null-terminated names, disable -C\n\
355 --exclude=PATTERN exclude files, given as a globbing PATTERN\n\
356 -X, --exclude-from=FILE exclude globbing patterns listed in FILE\n\
357 -P, --absolute-names don't strip leading `/'s from file names\n\
358 -h, --dereference dump instead the files symlinks point to\n\
359 --no-recursion avoid descending automatically in directories\n\
360 -l, --one-file-system stay in local file system when creating archive\n\
361 -K, --starting-file=NAME begin at file NAME in the archive\n"),
365 -N, --newer=DATE only store files newer than DATE\n\
366 --newer-mtime compare date and time when data changed only\n\
367 --after-date=DATE same as -N\n"),
371 --backup[=CONTROL] backup before removal, choose version control\n\
372 --suffix=SUFFIX backup before removel, override usual suffix\n"),
376 Informative output:\n\
377 --help print this help, then exit\n\
378 --version print tar program version number, then exit\n\
379 -v, --verbose verbosely list files processed\n\
380 --checkpoint print directory names while reading the archive\n\
381 --totals print total bytes written while creating archive\n\
382 -R, --block-number show block number within archive with each message\n\
383 -w, --interactive ask for confirmation for every action\n\
384 --confirmation same as -w\n"),
388 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
389 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
391 t, numbered make numbered backups\n\
392 nil, existing numbered if numbered backups exist, simple otherwise\n\
393 never, simple always make simple backups\n"),
397 GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
398 is set in the environment, GNU extensions are disallowed with `--posix'.\n\
399 Support for POSIX is only partially implemented, don't count on it yet.\n\
400 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; and FILE may be a file\n\
401 or a device. *This* `tar' defaults to `-f%s -b%d'.\n"),
402 DEFAULT_ARCHIVE
, DEFAULT_BLOCKING
);
405 Report bugs to <bug-tar@gnu.org>.\n"),
411 /*----------------------------.
412 | Parse the options for tar. |
413 `----------------------------*/
415 /* Available option letters are DEHIJQY and aejnqy. Some are reserved:
417 y per-file gzip compression
418 Y per-block gzip compression */
420 #define OPTION_STRING \
421 "-01234567ABC:F:GK:L:MN:OPRST:UV:WX:Zb:cdf:g:hiklmoprstuvwxyz"
424 set_subcommand_option (enum subcommand subcommand
)
426 if (subcommand_option
!= UNKNOWN_SUBCOMMAND
427 && subcommand_option
!= subcommand
)
429 _("You may not specify more than one `-Acdtrux' option")));
431 subcommand_option
= subcommand
;
435 set_use_compress_program_option (const char *string
)
437 if (use_compress_program_option
&& strcmp (use_compress_program_option
, string
) != 0)
438 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
440 use_compress_program_option
= string
;
443 /* Ignore DUMMY (which will always be null in practice), and add
444 PATTERN to the proper set of patterns to be excluded -- either
445 patterns with slashes, or patterns without. */
447 add_filtered_exclude (struct exclude
*dummy
, char const *pattern
)
449 add_exclude ((strchr (pattern
, '/')
450 ? excluded_with_slash
451 : excluded_without_slash
),
456 decode_options (int argc
, char *const *argv
)
458 int optchar
; /* option letter */
459 int input_files
; /* number of input files */
460 const char *backup_suffix_string
;
461 const char *version_control_string
= NULL
;
463 /* Set some default option values. */
465 subcommand_option
= UNKNOWN_SUBCOMMAND
;
466 archive_format
= DEFAULT_FORMAT
;
467 blocking_factor
= DEFAULT_BLOCKING
;
468 record_size
= DEFAULT_BLOCKING
* BLOCKSIZE
;
469 excluded_with_slash
= new_exclude ();
470 excluded_without_slash
= new_exclude ();
471 newer_mtime_option
= TYPE_MINIMUM (time_t);
476 backup_suffix_string
= getenv ("SIMPLE_BACKUP_SUFFIX");
478 /* Convert old-style tar call by exploding option element and rearranging
479 options accordingly. */
481 if (argc
> 1 && argv
[1][0] != '-')
483 int new_argc
; /* argc value for rearranged arguments */
484 char **new_argv
; /* argv value for rearranged arguments */
485 char *const *in
; /* cursor into original argv */
486 char **out
; /* cursor into rearranged argv */
487 const char *letter
; /* cursor into old option letters */
488 char buffer
[3]; /* constructed option buffer */
489 const char *cursor
; /* cursor in OPTION_STRING */
491 /* Initialize a constructed option. */
496 /* Allocate a new argument array, and copy program name in it. */
498 new_argc
= argc
- 1 + strlen (argv
[1]);
499 new_argv
= (char **) xmalloc (new_argc
* sizeof (char *));
504 /* Copy each old letter option as a separate option, and have the
505 corresponding argument moved next to it. */
507 for (letter
= *in
++; *letter
; letter
++)
510 *out
++ = xstrdup (buffer
);
511 cursor
= strchr (OPTION_STRING
, *letter
);
512 if (cursor
&& cursor
[1] == ':')
514 if (in
< argv
+ argc
)
517 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
522 /* Copy all remaining options. */
524 while (in
< argv
+ argc
)
527 /* Replace the old option list by the new one. */
533 /* Parse all options and non-options as they appear. */
537 while (optchar
= getopt_long (argc
, argv
, OPTION_STRING
, long_options
, NULL
),
542 usage (TAREXIT_FAILURE
);
548 /* File name or non-parsed option, because of RETURN_IN_ORDER
549 ordering triggerred by the leading dash in OPTION_STRING. */
556 set_subcommand_option (CAT_SUBCOMMAND
);
559 case OBSOLETE_BLOCK_COMPRESS
:
560 WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
563 case OBSOLETE_BLOCKING_FACTOR
:
564 WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
570 if (! (xstrtoumax (optarg
, (char **) 0, 10, &u
, "") == LONGINT_OK
571 && u
== (blocking_factor
= u
)
572 && 0 < blocking_factor
573 && u
== (record_size
= u
* (size_t) BLOCKSIZE
) / BLOCKSIZE
))
574 USAGE_ERROR ((0, 0, _("Invalid blocking factor")));
578 case OBSOLETE_READ_FULL_RECORDS
:
580 _("Obsolete option name replaced by --read-full-records")));
584 /* Try to reblock input records. For reading 4.2BSD pipes. */
586 /* It would surely make sense to exchange -B and -R, but it seems
587 that -B has been used for a long while in Sun tar ans most
588 BSD-derived systems. This is a consequence of the block/record
589 terminology confusion. */
591 read_full_records_option
= 1;
595 set_subcommand_option (CREATE_SUBCOMMAND
);
604 set_subcommand_option (DIFF_SUBCOMMAND
);
608 if (archive_names
== allocated_archive_names
)
610 allocated_archive_names
*= 2;
611 archive_name_array
= (const char **)
612 xrealloc (archive_name_array
,
613 sizeof (const char *) * allocated_archive_names
);
615 archive_name_array
[archive_names
++] = optarg
;
619 /* Since -F is only useful with -M, make it implied. Run this
620 script at the end of each tape. */
622 info_script_option
= optarg
;
623 multi_volume_option
= 1;
627 listed_incremental_option
= optarg
;
628 after_date_option
= 1;
632 /* We are making an incremental dump (FIXME: are we?); save
633 directories at the beginning of the archive, and include in each
634 directory its contents. */
636 incremental_option
= 1;
640 /* Follow symbolic links. */
642 dereference_option
= 1;
646 /* Ignore zero blocks (eofs). This can't be the default,
647 because Unix tar writes two blocks of zeros, then pads out
648 the record with garbage. */
650 ignore_zeros_option
= 1;
654 /* Don't overwrite existing files. */
656 keep_old_files_option
= 1;
660 starting_file_option
= 1;
665 /* When dumping directories, don't dump files/subdirectories
666 that are on other filesystems. */
668 one_file_system_option
= 1;
674 if (xstrtoumax (optarg
, (char **) 0, 10, &u
, "") != LONG_MAX
)
675 USAGE_ERROR ((0, 0, _("Invalid tape length")));
676 tape_length_option
= 1024 * (tarlong
) u
;
677 multi_volume_option
= 1;
682 WARN ((0, 0, _("Obsolete option name replaced by --touch")));
690 /* Make multivolume archive: when we can't write any more into
691 the archive, re-open it, and continue writing. */
693 multi_volume_option
= 1;
698 after_date_option
= 1;
701 case NEWER_MTIME_OPTION
:
702 if (newer_mtime_option
!= TYPE_MINIMUM (time_t))
703 USAGE_ERROR ((0, 0, _("More than one threshold date")));
705 newer_mtime_option
= get_date (optarg
, (voidstar
) 0);
706 if (newer_mtime_option
== (time_t) -1)
707 USAGE_ERROR ((0, 0, _("Invalid date format `%s'"), optarg
));
710 #endif /* not MSDOS */
713 if (archive_format
== DEFAULT_FORMAT
)
714 archive_format
= V7_FORMAT
;
715 else if (archive_format
!= V7_FORMAT
)
716 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
720 to_stdout_option
= 1;
724 same_permissions_option
= 1;
727 case OBSOLETE_ABSOLUTE_NAMES
:
728 WARN ((0, 0, _("Obsolete option name replaced by --absolute-names")));
732 absolute_names_option
= 1;
736 set_subcommand_option (APPEND_SUBCOMMAND
);
739 case OBSOLETE_BLOCK_NUMBER
:
740 WARN ((0, 0, _("Obsolete option name replaced by --block-number")));
744 /* Print block numbers for debugging bad tar archives. */
746 /* It would surely make sense to exchange -B and -R, but it seems
747 that -B has been used for a long while in Sun tar ans most
748 BSD-derived systems. This is a consequence of the block/record
749 terminology confusion. */
751 block_number_option
= 1;
755 /* Names to extr are sorted. */
757 same_order_option
= 1;
765 set_subcommand_option (LIST_SUBCOMMAND
);
770 files_from_option
= optarg
;
774 set_subcommand_option (UPDATE_SUBCOMMAND
);
778 unlink_first_option
= 1;
786 volume_label_option
= optarg
;
790 interactive_option
= 1;
798 set_subcommand_option (EXTRACT_SUBCOMMAND
);
802 if (add_exclude_file (add_filtered_exclude
, NULL
, optarg
, '\n') != 0)
803 FATAL_ERROR ((0, errno
, "%s", optarg
));
807 set_use_compress_program_option ("bzip2");
811 set_use_compress_program_option ("gzip");
815 set_use_compress_program_option ("compress");
818 case OBSOLETE_VERSION_CONTROL
:
819 WARN ((0, 0, _("Obsolete option name replaced by --backup")));
825 version_control_string
= optarg
;
829 set_subcommand_option (DELETE_SUBCOMMAND
);
833 add_filtered_exclude (NULL
, optarg
);
837 if (! (strlen (optarg
) < GNAME_FIELD_SIZE
838 && gname_to_gid (optarg
, &group_option
)))
841 if (xstrtoumax (optarg
, (char **) 0, 10, &g
, "") == LONGINT_OK
845 FATAL_ERROR ((0, 0, _("Invalid group given on option")));
851 = mode_compile (optarg
,
852 MODE_MASK_EQUALS
| MODE_MASK_PLUS
| MODE_MASK_MINUS
);
853 if (mode_option
== MODE_INVALID
)
854 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
855 if (mode_option
== MODE_MEMORY_EXHAUSTED
)
856 FATAL_ERROR ((0, 0, _("Memory exhausted")));
859 case NO_RECURSE_OPTION
:
860 no_recurse_option
= 1;
864 filename_terminator
= '\0';
868 if (! (strlen (optarg
) < UNAME_FIELD_SIZE
869 && uname_to_uid (optarg
, &owner_option
)))
872 if (xstrtoumax (optarg
, (char **) 0, 10, &u
, "") == LONGINT_OK
876 FATAL_ERROR ((0, 0, _("Invalid owner given on option")));
881 #if OLDGNU_COMPATIBILITY
882 if (archive_format
== DEFAULT_FORMAT
)
883 archive_format
= GNU_FORMAT
;
884 else if (archive_format
!= GNU_FORMAT
)
885 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
887 if (archive_format
== DEFAULT_FORMAT
)
888 archive_format
= POSIX_FORMAT
;
889 else if (archive_format
!= POSIX_FORMAT
)
890 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
894 case PRESERVE_OPTION
:
895 same_permissions_option
= 1;
896 same_order_option
= 1;
899 case RECORD_SIZE_OPTION
:
902 if (! (xstrtoumax (optarg
, (char **) 0, 10, &u
, "") == LONG_MAX
904 USAGE_ERROR ((0, 0, _("Invalid record size")));
906 if (record_size
% BLOCKSIZE
!= 0)
907 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
909 blocking_factor
= record_size
/ BLOCKSIZE
;
913 case RSH_COMMAND_OPTION
:
914 rsh_command_option
= optarg
;
919 backup_suffix_string
= optarg
;
922 case VOLNO_FILE_OPTION
:
923 volno_file_option
= optarg
;
926 case USE_COMPRESS_PROGRAM_OPTION
:
927 set_use_compress_program_option (optarg
);
941 int device
= optchar
- '0';
943 static char buf
[sizeof DEVICE_PREFIX
+ 10];
946 density
= getopt_long (argc
, argv
, "lmh", NULL
, NULL
);
947 strcpy (buf
, DEVICE_PREFIX
);
948 cursor
= buf
+ strlen (buf
);
950 #ifdef DENSITY_LETTER
952 sprintf (cursor
, "%d%c", device
, density
);
954 #else /* not DENSITY_LETTER */
981 usage (TAREXIT_FAILURE
);
983 sprintf (cursor
, "%d", device
);
985 #endif /* not DENSITY_LETTER */
987 if (archive_names
== allocated_archive_names
)
989 allocated_archive_names
*= 2;
990 archive_name_array
= (const char **)
991 xrealloc (archive_name_array
,
992 sizeof (const char *) * allocated_archive_names
);
994 archive_name_array
[archive_names
++] = buf
;
996 /* FIXME: How comes this works for many archives when buf is
1001 #else /* not DEVICE_PREFIX */
1004 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1006 #endif /* not DEVICE_PREFIX */
1009 /* Process trivial options. */
1013 printf ("tar (GNU %s) %s\n", PACKAGE
, VERSION
);
1016 Copyright 1988, 92,93,94,95,96,97,98, 1999 Free Software Foundation, Inc.\n"),
1019 This is free software; see the source for copying conditions. There is NO\n\
1020 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"),
1024 Written by John Gilmore and Jay Fenlason.\n"),
1026 exit (TAREXIT_SUCCESS
);
1030 usage (TAREXIT_SUCCESS
);
1032 /* Derive option values and check option consistency. */
1034 if (archive_format
== DEFAULT_FORMAT
)
1036 #if OLDGNU_COMPATIBILITY
1037 archive_format
= OLDGNU_FORMAT
;
1039 archive_format
= GNU_FORMAT
;
1043 if (archive_format
== GNU_FORMAT
&& getenv ("POSIXLY_CORRECT"))
1044 archive_format
= POSIX_FORMAT
;
1046 if ((volume_label_option
!= NULL
1047 || incremental_option
|| multi_volume_option
|| sparse_option
)
1048 && archive_format
!= OLDGNU_FORMAT
&& archive_format
!= GNU_FORMAT
)
1050 _("GNU features wanted on incompatible archive format")));
1052 if (archive_names
== 0)
1054 /* If no archive file name given, try TAPE from the environment, or
1055 else, DEFAULT_ARCHIVE from the configuration process. */
1058 archive_name_array
[0] = getenv ("TAPE");
1059 if (archive_name_array
[0] == NULL
)
1060 archive_name_array
[0] = DEFAULT_ARCHIVE
;
1063 /* Allow multiple archives only with `-M'. */
1065 if (archive_names
> 1 && !multi_volume_option
)
1067 _("Multiple archive files requires `-M' option")));
1069 if (listed_incremental_option
1070 && newer_mtime_option
!= TYPE_MINIMUM (time_t))
1072 _("Cannot combine --listed-incremental with --newer")));
1074 /* If ready to unlink hierarchies, so we are for simpler files. */
1075 if (recursive_unlink_option
)
1076 unlink_first_option
= 1;
1078 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1079 explicit or implied, is used correctly. */
1081 switch (subcommand_option
)
1083 case CREATE_SUBCOMMAND
:
1084 if (input_files
== 0 && !files_from_option
)
1086 _("Cowardly refusing to create an empty archive")));
1089 case EXTRACT_SUBCOMMAND
:
1090 case LIST_SUBCOMMAND
:
1091 case DIFF_SUBCOMMAND
:
1092 for (archive_name_cursor
= archive_name_array
;
1093 archive_name_cursor
< archive_name_array
+ archive_names
;
1094 archive_name_cursor
++)
1095 if (!strcmp (*archive_name_cursor
, "-"))
1096 request_stdin ("-f");
1099 case CAT_SUBCOMMAND
:
1100 case UPDATE_SUBCOMMAND
:
1101 case APPEND_SUBCOMMAND
:
1102 for (archive_name_cursor
= archive_name_array
;
1103 archive_name_cursor
< archive_name_array
+ archive_names
;
1104 archive_name_cursor
++)
1105 if (!strcmp (*archive_name_cursor
, "-"))
1107 _("Options `-Aru' are incompatible with `-f -'")));
1113 archive_name_cursor
= archive_name_array
;
1115 /* Prepare for generating backup names. */
1117 if (backup_suffix_string
)
1118 simple_backup_suffix
= xstrdup (backup_suffix_string
);
1121 backup_type
= xget_version ("--backup", version_control_string
);
1126 /*-----------------------.
1127 | Main routine for tar. |
1128 `-----------------------*/
1131 main (int argc
, char *const *argv
)
1133 program_name
= argv
[0];
1134 setlocale (LC_ALL
, "");
1135 bindtextdomain (PACKAGE
, LOCALEDIR
);
1136 textdomain (PACKAGE
);
1138 exit_status
= TAREXIT_SUCCESS
;
1139 filename_terminator
= '\n';
1141 /* Pre-allocate a few structures. */
1143 allocated_archive_names
= 10;
1144 archive_name_array
= (const char **)
1145 xmalloc (sizeof (const char *) * allocated_archive_names
);
1149 /* System V fork+wait does not work if SIGCHLD is ignored. */
1150 signal (SIGCHLD
, SIG_DFL
);
1155 /* Decode options. */
1157 decode_options (argc
, argv
);
1158 name_init (argc
, argv
);
1160 /* Main command execution. */
1162 if (volno_file_option
)
1163 init_volume_number ();
1165 switch (subcommand_option
)
1167 case UNKNOWN_SUBCOMMAND
:
1169 _("You must specify one of the `-Acdtrux' options")));
1171 case CAT_SUBCOMMAND
:
1172 case UPDATE_SUBCOMMAND
:
1173 case APPEND_SUBCOMMAND
:
1177 case DELETE_SUBCOMMAND
:
1178 delete_archive_members ();
1181 case CREATE_SUBCOMMAND
:
1186 print_total_written ();
1189 case EXTRACT_SUBCOMMAND
:
1191 read_and (extract_archive
);
1194 case LIST_SUBCOMMAND
:
1195 read_and (list_archive
);
1198 case DIFF_SUBCOMMAND
:
1200 read_and (diff_archive
);
1204 if (volno_file_option
)
1205 closeout_volume_number ();
1207 /* Dispose of allocated memory, and return. */
1209 free (archive_name_array
);
1212 if (exit_status
== TAREXIT_FAILURE
)
1213 error (0, 0, _("Error exit delayed from previous errors"));