1 /* A tar (tape archiver) program.
2 Copyright (C) 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 Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* The following causes "common.h" to produce definitions of all the global
24 variables, rather than just "extern" declarations of them. GNU tar does
25 depend on the system loader to preset all GLOBAL variables to neutral (or
26 zero) values, explicit initialisation is usually not done. */
30 #include "backupfile.h"
31 enum backup_type
get_version ();
33 /* FIXME: We should use a conversion routine that does reasonable error
34 checking -- atol doesn't. For now, punt. */
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 | Check if STRING0 is the decimal representation of number, and store its |
55 | value. If not a decimal number, return 0. |
56 `------------------------------------------------------------------------*/
59 check_decimal (const char *string0
, uintmax_t *result
)
61 const char *string
= string0
;
78 uintmax_t v10
= value
* 10;
79 uintmax_t v10d
= v10
+ (*string
- '0');
80 if (v10
/ 10 != value
|| v10d
< v10
)
95 /*----------------------------------------------.
96 | Doesn't return if stdin already requested. |
97 `----------------------------------------------*/
99 /* Name of option using stdin. */
100 static const char *stdin_used_by
= NULL
;
103 request_stdin (const char *option
)
106 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
107 stdin_used_by
, option
));
109 stdin_used_by
= option
;
112 /*--------------------------------------------------------.
113 | Returns true if and only if the user typed 'y' or 'Y'. |
114 `--------------------------------------------------------*/
117 confirm (const char *message_action
, const char *message_name
)
119 static FILE *confirm_file
= NULL
;
123 if (archive
== 0 || stdin_used_by
)
124 confirm_file
= fopen (TTY_NAME
, "r");
127 request_stdin ("-w");
128 confirm_file
= stdin
;
132 FATAL_ERROR ((0, 0, _("Cannot read confirmation from user")));
135 fprintf (stdlis
, "%s %s?", message_action
, message_name
);
139 int reply
= getc (confirm_file
);
142 for (character
= reply
;
143 character
!= '\n' && character
!= EOF
;
144 character
= getc (confirm_file
))
146 return reply
== 'y' || reply
== 'Y';
152 /* For long options that unconditionally set a single flag, we have getopt
153 do it. For the others, we share the code for the equivalent short
154 named option, the name of which is stored in the otherwise-unused `val'
155 field of the `struct option'; for long options that have no equivalent
156 short option, we use nongraphic characters as pseudo short option
157 characters, starting at 2 and going upwards. */
159 #define BACKUP_OPTION 2
160 #define DELETE_OPTION 3
161 #define EXCLUDE_OPTION 4
162 #define GROUP_OPTION 5
163 #define MODE_OPTION 6
164 #define NEWER_MTIME_OPTION 7
165 #define NO_RECURSE_OPTION 8
166 #define NULL_OPTION 9
167 #define OWNER_OPTION 10
168 #define POSIX_OPTION 11
169 #define PRESERVE_OPTION 12
170 #define RECORD_SIZE_OPTION 13
171 #define RSH_COMMAND_OPTION 14
172 #define SUFFIX_OPTION 15
173 #define USE_COMPRESS_PROGRAM_OPTION 16
174 #define VOLNO_FILE_OPTION 17
176 /* Some cleanup is being made in GNU tar long options. Using old names is
177 allowed for a while, but will also send a warning to stderr. Take old
178 names out in 1.14, or in summer 1997, whichever happens last. We use
179 nongraphic characters as pseudo short option characters, starting at 31
180 and going downwards. */
182 #define OBSOLETE_ABSOLUTE_NAMES 31
183 #define OBSOLETE_BLOCK_COMPRESS 30
184 #define OBSOLETE_BLOCKING_FACTOR 29
185 #define OBSOLETE_BLOCK_NUMBER 28
186 #define OBSOLETE_READ_FULL_RECORDS 27
187 #define OBSOLETE_TOUCH 26
188 #define OBSOLETE_VERSION_CONTROL 25
190 /* If nonzero, display usage information and exit. */
191 static int show_help
= 0;
193 /* If nonzero, print the version on standard output and exit. */
194 static int show_version
= 0;
196 struct option long_options
[] =
198 {"absolute-names", no_argument
, NULL
, 'P'},
199 {"absolute-paths", no_argument
, NULL
, OBSOLETE_ABSOLUTE_NAMES
},
200 {"after-date", required_argument
, NULL
, 'N'},
201 {"append", no_argument
, NULL
, 'r'},
202 {"atime-preserve", no_argument
, &atime_preserve_option
, 1},
203 {"backup", optional_argument
, NULL
, BACKUP_OPTION
},
204 {"block-compress", no_argument
, NULL
, OBSOLETE_BLOCK_COMPRESS
},
205 {"block-number", no_argument
, NULL
, 'R'},
206 {"block-size", required_argument
, NULL
, OBSOLETE_BLOCKING_FACTOR
},
207 {"blocking-factor", required_argument
, NULL
, 'b'},
208 {"bunzip2", no_argument
, NULL
, 'y'},
209 {"bzip2", no_argument
, NULL
, 'y'},
210 {"catenate", no_argument
, NULL
, 'A'},
211 {"checkpoint", no_argument
, &checkpoint_option
, 1},
212 {"compare", no_argument
, NULL
, 'd'},
213 {"compress", no_argument
, NULL
, 'Z'},
214 {"concatenate", no_argument
, NULL
, 'A'},
215 {"confirmation", no_argument
, NULL
, 'w'},
216 /* FIXME: --selective as a synonym for --confirmation? */
217 {"create", no_argument
, NULL
, 'c'},
218 {"delete", no_argument
, NULL
, DELETE_OPTION
},
219 {"dereference", no_argument
, NULL
, 'h'},
220 {"diff", no_argument
, NULL
, 'd'},
221 {"directory", required_argument
, NULL
, 'C'},
222 {"ending-file", required_argument
, NULL
, 'E'},
223 {"exclude", required_argument
, NULL
, EXCLUDE_OPTION
},
224 {"exclude-from", required_argument
, NULL
, 'X'},
225 {"extract", no_argument
, NULL
, 'x'},
226 {"file", required_argument
, NULL
, 'f'},
227 {"files-from", required_argument
, NULL
, 'T'},
228 {"force-local", no_argument
, &force_local_option
, 1},
229 {"get", no_argument
, NULL
, 'x'},
230 {"group", required_argument
, NULL
, GROUP_OPTION
},
231 {"gunzip", no_argument
, NULL
, 'z'},
232 {"gzip", no_argument
, NULL
, 'z'},
233 {"help", no_argument
, &show_help
, 1},
234 {"ignore-failed-read", no_argument
, &ignore_failed_read_option
, 1},
235 {"ignore-zeros", no_argument
, NULL
, 'i'},
236 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
237 {"incremental", no_argument
, NULL
, 'G'},
238 {"info-script", required_argument
, NULL
, 'F'},
239 {"interactive", no_argument
, NULL
, 'w'},
240 {"keep-old-files", no_argument
, NULL
, 'k'},
241 {"label", required_argument
, NULL
, 'V'},
242 {"list", no_argument
, NULL
, 't'},
243 {"listed-incremental", required_argument
, NULL
, 'g'},
244 {"mode", required_argument
, NULL
, MODE_OPTION
},
245 {"modification-time", no_argument
, NULL
, OBSOLETE_TOUCH
},
246 {"multi-volume", no_argument
, NULL
, 'M'},
247 {"new-volume-script", required_argument
, NULL
, 'F'},
248 {"newer", required_argument
, NULL
, 'N'},
249 {"newer-mtime", required_argument
, NULL
, NEWER_MTIME_OPTION
},
250 {"null", no_argument
, NULL
, NULL_OPTION
},
251 {"no-recursion", no_argument
, NULL
, NO_RECURSE_OPTION
},
252 {"numeric-owner", no_argument
, &numeric_owner_option
, 1},
253 {"old-archive", no_argument
, NULL
, 'o'},
254 {"one-file-system", no_argument
, NULL
, 'l'},
255 {"owner", required_argument
, NULL
, OWNER_OPTION
},
256 {"portability", no_argument
, NULL
, 'o'},
257 {"posix", no_argument
, NULL
, POSIX_OPTION
},
258 {"preserve", no_argument
, NULL
, PRESERVE_OPTION
},
259 {"preserve-order", no_argument
, NULL
, 's'},
260 {"preserve-permissions", no_argument
, NULL
, 'p'},
261 {"recursive-unlink", no_argument
, &recursive_unlink_option
, 1},
262 {"read-full-blocks", no_argument
, NULL
, OBSOLETE_READ_FULL_RECORDS
},
263 {"read-full-records", no_argument
, NULL
, 'B'},
264 /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
265 {"record-number", no_argument
, NULL
, OBSOLETE_BLOCK_NUMBER
},
266 {"record-size", required_argument
, NULL
, RECORD_SIZE_OPTION
},
267 {"remove-files", no_argument
, &remove_files_option
, 1},
268 {"rsh-command", required_argument
, NULL
, RSH_COMMAND_OPTION
},
269 {"same-order", no_argument
, NULL
, 's'},
270 {"same-owner", no_argument
, &same_owner_option
, 1},
271 {"same-permissions", no_argument
, NULL
, 'p'},
272 {"show-omitted-dirs", no_argument
, &show_omitted_dirs_option
, 1},
273 {"sparse", no_argument
, NULL
, 'S'},
274 {"starting-file", required_argument
, NULL
, 'K'},
275 {"suffix", required_argument
, NULL
, SUFFIX_OPTION
},
276 {"tape-length", required_argument
, NULL
, 'L'},
277 {"to-stdout", no_argument
, NULL
, 'O'},
278 {"totals", no_argument
, &totals_option
, 1},
279 {"touch", no_argument
, NULL
, 'm'},
280 {"uncompress", no_argument
, NULL
, 'Z'},
281 {"unbzip2", no_argument
, NULL
, 'y'},
282 {"ungzip", no_argument
, NULL
, 'z'},
283 {"unlink-first", no_argument
, NULL
, 'U'},
284 {"update", no_argument
, NULL
, 'u'},
285 {"use-compress-program", required_argument
, NULL
, USE_COMPRESS_PROGRAM_OPTION
},
286 {"verbose", no_argument
, NULL
, 'v'},
287 {"verify", no_argument
, NULL
, 'W'},
288 {"version", no_argument
, &show_version
, 1},
289 {"version-control", required_argument
, NULL
, OBSOLETE_VERSION_CONTROL
},
290 {"volno-file", required_argument
, NULL
, VOLNO_FILE_OPTION
},
295 /*---------------------------------------------.
296 | Print a usage message and exit with STATUS. |
297 `---------------------------------------------*/
302 if (status
!= TAREXIT_SUCCESS
)
303 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
308 GNU `tar' saves many files together into a single tape or disk archive, and\n\
309 can restore individual files from the archive.\n"),
311 printf (_("\nUsage: %s [OPTION]... [FILE]...\n"), program_name
);
314 If a long option shows an argument as mandatory, then it is mandatory\n\
315 for the equivalent short option also. Similarly for optional arguments.\n"),
319 Main operation mode:\n\
320 -t, --list list the contents of an archive\n\
321 -x, --extract, --get extract files from an archive\n\
322 -c, --create create a new archive\n\
323 -d, --diff, --compare find differences between archive and file system\n\
324 -r, --append append files to the end of an archive\n\
325 -u, --update only append files newer than copy in archive\n\
326 -A, --catenate append tar files to an archive\n\
327 --concatenate same as -A\n\
328 --delete delete from the archive (not on mag tapes!)\n"),
332 Operation modifiers:\n\
333 -W, --verify attempt to verify the archive after writing it\n\
334 --remove-files remove files after adding them to the archive\n\
335 -k, --keep-old-files don't overwrite existing files when extracting\n\
336 -U, --unlink-first remove each file prior to extracting over it\n\
337 --recursive-unlink empty hierarchies prior to extracting directory\n\
338 -S, --sparse handle sparse files efficiently\n\
339 -O, --to-stdout extract files to standard output\n\
340 -G, --incremental handle old GNU-format incremental backup\n\
341 -g, --listed-incremental handle new GNU-format incremental backup\n\
342 --ignore-failed-read do not exit with nonzero on unreadable files\n"),
346 Handling of file attributes:\n\
347 --owner=NAME force NAME as owner for added files\n\
348 --group=NAME force NAME as group for added files\n\
349 --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
350 --atime-preserve don't change access times on dumped files\n\
351 -m, --modification-time don't extract file modified time\n\
352 --same-owner try extracting files with the same ownership\n\
353 --numeric-owner always use numbers for user/group names\n\
354 -p, --same-permissions extract all protection information\n\
355 --preserve-permissions same as -p\n\
356 -s, --same-order sort names to extract to match archive\n\
357 --preserve-order same as -s\n\
358 --preserve same as both -p and -s\n"),
362 Device selection and switching:\n\
363 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
364 --force-local archive file is local even if has a colon\n\
365 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
366 -[0-7][lmh] specify drive and density\n\
367 -M, --multi-volume create/list/extract multi-volume archive\n\
368 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
369 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
370 --new-volume-script=FILE same as -F FILE\n\
371 --volno-file=FILE use/update the volume number in FILE\n"),
376 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
377 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
378 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
379 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
383 Archive format selection:\n\
384 -V, --label=NAME create archive with volume name NAME\n\
385 PATTERN at list/extract time, a globbing PATTERN\n\
386 -o, --old-archive, --portability write a V7 format archive\n\
387 --posix write a POSIX conformant archive\n\
388 -y, --bzip2, --unbzip2 filter the archive through bzip2\n\
389 -z, --gzip, --ungzip filter the archive through gzip\n\
390 -Z, --compress, --uncompress filter the archive through compress\n\
391 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
395 Local file selection:\n\
396 -C, --directory=DIR change to directory DIR\n\
397 -T, --files-from=NAME get names to extract or create from file NAME\n\
398 --null -T reads null-terminated names, disable -C\n\
399 --exclude=PATTERN exclude files, given as a globbing PATTERN\n\
400 -X, --exclude-from=FILE exclude globbing patterns listed in FILE\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 -E, --ending-file=NAME end reading the archive before file NAME\n\
406 -K, --starting-file=NAME begin at file NAME in the archive\n"),
410 -N, --newer=DATE only store files newer than DATE\n\
411 --newer-mtime compare date and time when data changed only\n\
412 --after-date=DATE same as -N\n"),
416 --backup[=CONTROL] backup before removal, choose version control\n\
417 --suffix=SUFFIX backup before removel, override usual suffix\n"),
421 Informative output:\n\
422 --help print this help, then exit\n\
423 --version print tar program version number, then exit\n\
424 -v, --verbose verbosely list files processed\n\
425 --checkpoint print directory names while reading the archive\n\
426 --totals print total bytes written while creating archive\n\
427 -R, --block-number show block number within archive with each message\n\
428 -w, --interactive ask for confirmation for every action\n\
429 --confirmation same as -w\n"),
433 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
434 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
436 t, numbered make numbered backups\n\
437 nil, existing numbered if numbered backups exist, simple otherwise\n\
438 never, simple always make simple backups\n"),
442 GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
443 is set in the environment, GNU extensions are disallowed with `--posix'.\n\
444 Support for POSIX is only partially implemented, don't count on it yet.\n\
445 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; and FILE may be a file\n\
446 or a device. *This* `tar' defaults to `-f%s -b%d'.\n"),
447 DEFAULT_ARCHIVE
, DEFAULT_BLOCKING
);
450 Report bugs to <tar-bugs@gnu.ai.mit.edu>.\n"),
456 /*----------------------------.
457 | Parse the options for tar. |
458 `----------------------------*/
460 /* Available option letters are DHIJQY and aejnqy. Some are reserved:
462 y per-file gzip compression
463 Y per-block gzip compression */
465 #define OPTION_STRING \
466 "-01234567ABC:E:F:GK:L:MN:OPRST:UV:WX:Zb:cdf:g:hiklmoprstuvwxyz"
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 *const *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
;
496 /* Set some default option values. */
498 subcommand_option
= UNKNOWN_SUBCOMMAND
;
499 archive_format
= DEFAULT_FORMAT
;
500 blocking_factor
= DEFAULT_BLOCKING
;
501 record_size
= DEFAULT_BLOCKING
* BLOCKSIZE
;
506 backup_suffix_string
= getenv ("SIMPLE_BACKUP_SUFFIX");
507 version_control_string
= getenv ("VERSION_CONTROL");
509 /* Convert old-style tar call by exploding option element and rearranging
510 options accordingly. */
512 if (argc
> 1 && argv
[1][0] != '-')
514 int new_argc
; /* argc value for rearranged arguments */
515 char **new_argv
; /* argv value for rearranged arguments */
516 char *const *in
; /* cursor into original argv */
517 char **out
; /* cursor into rearranged argv */
518 const char *letter
; /* cursor into old option letters */
519 char buffer
[3]; /* constructed option buffer */
520 const char *cursor
; /* cursor in OPTION_STRING */
522 /* Initialize a constructed option. */
527 /* Allocate a new argument array, and copy program name in it. */
529 new_argc
= argc
- 1 + strlen (argv
[1]);
530 new_argv
= (char **) xmalloc (new_argc
* sizeof (char *));
535 /* Copy each old letter option as a separate option, and have the
536 corresponding argument moved next to it. */
538 for (letter
= *in
++; *letter
; letter
++)
541 *out
++ = xstrdup (buffer
);
542 cursor
= strchr (OPTION_STRING
, *letter
);
543 if (cursor
&& cursor
[1] == ':')
545 if (in
< argv
+ argc
)
548 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
553 /* Copy all remaining options. */
555 while (in
< argv
+ argc
)
558 /* Replace the old option list by the new one. */
564 /* Parse all options and non-options as they appear. */
568 while (optchar
= getopt_long (argc
, argv
, OPTION_STRING
, long_options
, NULL
),
573 usage (TAREXIT_FAILURE
);
579 /* File name or non-parsed option, because of RETURN_IN_ORDER
580 ordering triggerred by the leading dash in OPTION_STRING. */
587 set_subcommand_option (CAT_SUBCOMMAND
);
590 case OBSOLETE_BLOCK_COMPRESS
:
591 WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
594 case OBSOLETE_BLOCKING_FACTOR
:
595 WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
599 blocking_factor
= intconv (optarg
);
600 record_size
= blocking_factor
* (size_t) BLOCKSIZE
;
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 ending_file_option
= optarg
;
637 if (archive_names
== allocated_archive_names
)
639 allocated_archive_names
*= 2;
640 archive_name_array
= (const char **)
641 xrealloc (archive_name_array
,
642 sizeof (const char *) * allocated_archive_names
);
644 archive_name_array
[archive_names
++] = optarg
;
648 /* Since -F is only useful with -M, make it implied. Run this
649 script at the end of each tape. */
651 info_script_option
= optarg
;
652 multi_volume_option
= 1;
656 listed_incremental_option
= optarg
;
660 /* We are making an incremental dump (FIXME: are we?); save
661 directories at the beginning of the archive, and include in each
662 directory its contents. */
664 incremental_option
= 1;
668 /* Follow symbolic links. */
670 dereference_option
= 1;
674 /* Ignore zero blocks (eofs). This can't be the default,
675 because Unix tar writes two blocks of zeros, then pads out
676 the record with garbage. */
678 ignore_zeros_option
= 1;
682 /* Don't overwrite existing files. */
684 keep_old_files_option
= 1;
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;
700 clear_tarlong (tape_length_option
);
701 add_to_tarlong (tape_length_option
, intconv (optarg
));
702 mult_tarlong (tape_length_option
, 1024);
703 multi_volume_option
= 1;
707 WARN ((0, 0, _("Obsolete option name replaced by --touch")));
715 /* Make multivolume archive: when we can't write any more into
716 the archive, re-open it, and continue writing. */
718 multi_volume_option
= 1;
723 after_date_option
= 1;
726 case NEWER_MTIME_OPTION
:
727 if (newer_mtime_option
)
728 USAGE_ERROR ((0, 0, _("More than one threshold date")));
730 newer_mtime_option
= get_date (optarg
, (voidstar
) 0);
731 if (newer_mtime_option
== (time_t) -1)
732 USAGE_ERROR ((0, 0, _("Invalid date format `%s'"), optarg
));
735 #endif /* not MSDOS */
738 if (archive_format
== DEFAULT_FORMAT
)
739 archive_format
= V7_FORMAT
;
740 else if (archive_format
!= V7_FORMAT
)
741 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
745 to_stdout_option
= 1;
749 same_permissions_option
= 1;
752 case OBSOLETE_ABSOLUTE_NAMES
:
753 WARN ((0, 0, _("Obsolete option name replaced by --absolute-names")));
757 absolute_names_option
= 1;
761 set_subcommand_option (APPEND_SUBCOMMAND
);
764 case OBSOLETE_BLOCK_NUMBER
:
765 WARN ((0, 0, _("Obsolete option name replaced by --block-number")));
769 /* Print block numbers for debugging bad tar archives. */
771 /* It would surely make sense to exchange -B and -R, but it seems
772 that -B has been used for a long while in Sun tar ans most
773 BSD-derived systems. This is a consequence of the block/record
774 terminology confusion. */
776 block_number_option
= 1;
780 /* Names to extr are sorted. */
782 same_order_option
= 1;
790 set_subcommand_option (LIST_SUBCOMMAND
);
795 files_from_option
= optarg
;
799 set_subcommand_option (UPDATE_SUBCOMMAND
);
803 unlink_first_option
= 1;
811 volume_label_option
= optarg
;
815 interactive_option
= 1;
823 set_subcommand_option (EXTRACT_SUBCOMMAND
);
828 add_exclude_file (optarg
);
832 set_use_compress_program_option ("bzip2");
836 set_use_compress_program_option ("gzip");
840 set_use_compress_program_option ("compress");
843 case OBSOLETE_VERSION_CONTROL
:
844 WARN ((0, 0, _("Obsolete option name replaced by --backup")));
850 version_control_string
= optarg
;
854 set_subcommand_option (DELETE_SUBCOMMAND
);
859 add_exclude (optarg
);
863 if (! (strlen (optarg
) < GNAME_FIELD_SIZE
864 && gname_to_gid (optarg
, &group_option
)))
867 if (!check_decimal (optarg
, &g
) || g
!= (gid_t
) g
)
868 ERROR ((TAREXIT_FAILURE
, 0, _("Invalid group given on option")));
876 = mode_compile (optarg
,
877 MODE_MASK_EQUALS
| MODE_MASK_PLUS
| MODE_MASK_MINUS
);
878 if (mode_option
== MODE_INVALID
)
879 ERROR ((TAREXIT_FAILURE
, 0, _("Invalid mode given on option")));
880 if (mode_option
== MODE_MEMORY_EXHAUSTED
)
881 ERROR ((TAREXIT_FAILURE
, 0, _("Memory exhausted")));
884 case NO_RECURSE_OPTION
:
885 no_recurse_option
= 1;
889 filename_terminator
= '\0';
893 if (! (strlen (optarg
) < UNAME_FIELD_SIZE
894 && uname_to_uid (optarg
, &owner_option
)))
897 if (!check_decimal (optarg
, &u
) || u
!= (uid_t
) u
)
898 ERROR ((TAREXIT_FAILURE
, 0, _("Invalid owner given on option")));
905 #if OLDGNU_COMPATIBILITY
906 if (archive_format
== DEFAULT_FORMAT
)
907 archive_format
= GNU_FORMAT
;
908 else if (archive_format
!= GNU_FORMAT
)
909 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
911 if (archive_format
== DEFAULT_FORMAT
)
912 archive_format
= POSIX_FORMAT
;
913 else if (archive_format
!= POSIX_FORMAT
)
914 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
918 case PRESERVE_OPTION
:
919 same_permissions_option
= 1;
920 same_order_option
= 1;
923 case RECORD_SIZE_OPTION
:
924 record_size
= intconv (optarg
);
925 if (record_size
% BLOCKSIZE
!= 0)
926 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
928 blocking_factor
= record_size
/ BLOCKSIZE
;
931 case RSH_COMMAND_OPTION
:
932 rsh_command_option
= optarg
;
937 backup_suffix_string
= optarg
;
940 case VOLNO_FILE_OPTION
:
941 volno_file_option
= optarg
;
944 case USE_COMPRESS_PROGRAM_OPTION
:
945 set_use_compress_program_option (optarg
);
959 int device
= optchar
- '0';
961 static char buf
[sizeof DEVICE_PREFIX
+ 10];
964 density
= getopt_long (argc
, argv
, "lmh", NULL
, NULL
);
965 strcpy (buf
, DEVICE_PREFIX
);
966 cursor
= buf
+ strlen (buf
);
968 #ifdef DENSITY_LETTER
970 sprintf (cursor
, "%d%c", device
, density
);
972 #else /* not DENSITY_LETTER */
999 usage (TAREXIT_FAILURE
);
1001 sprintf (cursor
, "%d", device
);
1003 #endif /* not DENSITY_LETTER */
1005 if (archive_names
== allocated_archive_names
)
1007 allocated_archive_names
*= 2;
1008 archive_name_array
= (const char **)
1009 xrealloc (archive_name_array
,
1010 sizeof (const char *) * allocated_archive_names
);
1012 archive_name_array
[archive_names
++] = buf
;
1014 /* FIXME: How comes this works for many archives when buf is
1019 #else /* not DEVICE_PREFIX */
1022 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1024 #endif /* not DEVICE_PREFIX */
1027 /* Process trivial options. */
1031 printf ("tar (GNU %s) %s\n", PACKAGE
, VERSION
);
1034 Copyright (C) 1988, 92,93,94,95,96,97,98, 1999 Free Software Foundation, Inc.\n"),
1037 This is free software; see the source for copying conditions. There is NO\n\
1038 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"),
1042 Written by John Gilmore and Jay Fenlason.\n"),
1044 exit (TAREXIT_SUCCESS
);
1048 usage (TAREXIT_SUCCESS
);
1050 /* Derive option values and check option consistency. */
1052 if (archive_format
== DEFAULT_FORMAT
)
1054 #if OLDGNU_COMPATIBILITY
1055 archive_format
= OLDGNU_FORMAT
;
1057 archive_format
= GNU_FORMAT
;
1061 if (archive_format
== GNU_FORMAT
&& getenv ("POSIXLY_CORRECT"))
1062 archive_format
= POSIX_FORMAT
;
1064 if ((volume_label_option
!= NULL
1065 || incremental_option
|| multi_volume_option
|| sparse_option
)
1066 && archive_format
!= OLDGNU_FORMAT
&& archive_format
!= GNU_FORMAT
)
1068 _("GNU features wanted on incompatible archive format")));
1070 if (archive_names
== 0)
1072 /* If no archive file name given, try TAPE from the environment, or
1073 else, DEFAULT_ARCHIVE from the configuration process. */
1076 archive_name_array
[0] = getenv ("TAPE");
1077 if (archive_name_array
[0] == NULL
)
1078 archive_name_array
[0] = DEFAULT_ARCHIVE
;
1081 /* Allow multiple archives only with `-M'. */
1083 if (archive_names
> 1 && !multi_volume_option
)
1085 _("Multiple archive files requires `-M' option")));
1087 /* If ready to unlink hierarchies, so we are for simpler files. */
1088 if (recursive_unlink_option
)
1089 unlink_first_option
= 1;
1091 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1092 explicit or implied, is used correctly. */
1094 switch (subcommand_option
)
1096 case CREATE_SUBCOMMAND
:
1097 if (input_files
== 0 && !files_from_option
)
1099 _("Cowardly refusing to create an empty archive")));
1102 case EXTRACT_SUBCOMMAND
:
1103 case LIST_SUBCOMMAND
:
1104 case DIFF_SUBCOMMAND
:
1105 for (archive_name_cursor
= archive_name_array
;
1106 archive_name_cursor
< archive_name_array
+ archive_names
;
1107 archive_name_cursor
++)
1108 if (!strcmp (*archive_name_cursor
, "-"))
1109 request_stdin ("-f");
1112 case CAT_SUBCOMMAND
:
1113 case UPDATE_SUBCOMMAND
:
1114 case APPEND_SUBCOMMAND
:
1115 for (archive_name_cursor
= archive_name_array
;
1116 archive_name_cursor
< archive_name_array
+ archive_names
;
1117 archive_name_cursor
++)
1118 if (!strcmp (*archive_name_cursor
, "-"))
1120 _("Options `-Aru' are incompatible with `-f -'")));
1126 archive_name_cursor
= archive_name_array
;
1128 /* Prepare for generating backup names. */
1130 if (backup_suffix_string
)
1131 simple_backup_suffix
= xstrdup (backup_suffix_string
);
1134 backup_type
= get_version (version_control_string
);
1139 /*-----------------------.
1140 | Main routine for tar. |
1141 `-----------------------*/
1144 main (int argc
, char *const *argv
)
1146 program_name
= argv
[0];
1147 setlocale (LC_ALL
, "");
1148 bindtextdomain (PACKAGE
, LOCALEDIR
);
1149 textdomain (PACKAGE
);
1151 exit_status
= TAREXIT_SUCCESS
;
1152 filename_terminator
= '\n';
1154 /* Pre-allocate a few structures. */
1156 allocated_archive_names
= 10;
1157 archive_name_array
= (const char **)
1158 xmalloc (sizeof (const char *) * allocated_archive_names
);
1163 /* Decode options. */
1165 decode_options (argc
, argv
);
1166 name_init (argc
, argv
);
1168 /* Main command execution. */
1170 if (volno_file_option
)
1171 init_volume_number ();
1173 switch (subcommand_option
)
1175 case UNKNOWN_SUBCOMMAND
:
1177 _("You must specify one of the `-Acdtrux' options")));
1179 case CAT_SUBCOMMAND
:
1180 case UPDATE_SUBCOMMAND
:
1181 case APPEND_SUBCOMMAND
:
1185 case DELETE_SUBCOMMAND
:
1186 delete_archive_members ();
1189 case CREATE_SUBCOMMAND
:
1191 init_total_written ();
1197 print_total_written ();
1200 case EXTRACT_SUBCOMMAND
:
1202 read_and (extract_archive
);
1205 case LIST_SUBCOMMAND
:
1206 read_and (list_archive
);
1209 case DIFF_SUBCOMMAND
:
1211 read_and (diff_archive
);
1215 if (volno_file_option
)
1216 closeout_volume_number ();
1218 /* Dispose of allocated memory, and return. */
1220 free (archive_name_array
);
1223 if (exit_status
== TAREXIT_FAILURE
)
1224 error (0, 0, _("Error exit delayed from previous errors"));