]> Dogcows Code - chaz/tar/blob - src/tar.c
("quotearg.h"): New include.
[chaz/tar] / src / tar.c
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.
4
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
8 version.
9
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.
14
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. */
18
19 #include "system.h"
20
21 #include <getopt.h>
22
23 #include <signal.h>
24 #if ! defined SIGCHLD && defined SIGCLD
25 # define SIGCHLD SIGCLD
26 #endif
27
28 #include <time.h>
29 #ifndef time
30 time_t time ();
31 #endif
32
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. */
37 #define GLOBAL
38 #include "common.h"
39
40 #include "quotearg.h"
41 #include "xstrtol.h"
42
43 time_t get_date ();
44
45 /* Local declarations. */
46
47 #ifndef DEFAULT_ARCHIVE
48 # define DEFAULT_ARCHIVE "tar.out"
49 #endif
50
51 #ifndef DEFAULT_BLOCKING
52 # define DEFAULT_BLOCKING 20
53 #endif
54
55 static void usage PARAMS ((int)) __attribute__ ((noreturn));
56 \f
57 /* Miscellaneous. */
58
59 /*----------------------------------------------.
60 | Doesn't return if stdin already requested. |
61 `----------------------------------------------*/
62
63 /* Name of option using stdin. */
64 static const char *stdin_used_by;
65
66 void
67 request_stdin (const char *option)
68 {
69 if (stdin_used_by)
70 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
71 stdin_used_by, option));
72
73 stdin_used_by = option;
74 }
75
76 /*--------------------------------------------------------.
77 | Returns true if and only if the user typed 'y' or 'Y'. |
78 `--------------------------------------------------------*/
79
80 int
81 confirm (const char *message_action, const char *message_name)
82 {
83 static FILE *confirm_file;
84 static int confirm_file_EOF;
85
86 if (!confirm_file)
87 {
88 if (archive == 0 || stdin_used_by)
89 {
90 confirm_file = fopen (TTY_NAME, "r");
91 if (! confirm_file)
92 open_fatal (TTY_NAME);
93 }
94 else
95 {
96 request_stdin ("-w");
97 confirm_file = stdin;
98 }
99 }
100
101 fprintf (stdlis, "%s %s?", message_action, quote (message_name));
102 fflush (stdlis);
103
104 {
105 int reply = confirm_file_EOF ? EOF : getc (confirm_file);
106 int character;
107
108 for (character = reply;
109 character != '\n';
110 character = getc (confirm_file))
111 if (character == EOF)
112 {
113 confirm_file_EOF = 1;
114 fputc ('\n', stdlis);
115 fflush (stdlis);
116 break;
117 }
118 return reply == 'y' || reply == 'Y';
119 }
120 }
121 \f
122 /* Options. */
123
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. */
130
131 enum
132 {
133 BACKUP_OPTION = CHAR_MAX + 1,
134 DELETE_OPTION,
135 EXCLUDE_OPTION,
136 GROUP_OPTION,
137 MODE_OPTION,
138 NEWER_MTIME_OPTION,
139 NO_RECURSE_OPTION,
140 NULL_OPTION,
141 OVERWRITE_OPTION,
142 OWNER_OPTION,
143 POSIX_OPTION,
144 PRESERVE_OPTION,
145 RECORD_SIZE_OPTION,
146 RSH_COMMAND_OPTION,
147 SUFFIX_OPTION,
148 USE_COMPRESS_PROGRAM_OPTION,
149 VOLNO_FILE_OPTION,
150
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. */
154
155 OBSOLETE_ABSOLUTE_NAMES,
156 OBSOLETE_BLOCK_COMPRESS,
157 OBSOLETE_BLOCKING_FACTOR,
158 OBSOLETE_BLOCK_NUMBER,
159 OBSOLETE_READ_FULL_RECORDS,
160 OBSOLETE_TOUCH,
161 OBSOLETE_VERSION_CONTROL
162 };
163
164 /* If nonzero, display usage information and exit. */
165 static int show_help;
166
167 /* If nonzero, print the version on standard output and exit. */
168 static int show_version;
169
170 struct option long_options[] =
171 {
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},
265
266 {0, 0, 0, 0}
267 };
268
269 /*---------------------------------------------.
270 | Print a usage message and exit with STATUS. |
271 `---------------------------------------------*/
272
273 static void
274 usage (int status)
275 {
276 if (status != TAREXIT_SUCCESS)
277 fprintf (stderr, _("Try `%s --help' for more information.\n"),
278 program_name);
279 else
280 {
281 fputs (_("\
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"),
284 stdout);
285 printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
286 \n\
287 Examples:\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);
292 fputs (_("\
293 \n\
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"),
296 stdout);
297 fputs(_("\
298 \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"),
309 stdout);
310 fputs (_("\
311 \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"),
325 stdout);
326 fputs (_("\
327 \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"),
343 stdout);
344 fputs (_("\
345 \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"),
356 stdout);
357 fputs (_("\
358 \n\
359 Device blocking:\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"),
364 stdout);
365 fputs (_("\
366 \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"),
376 stdout);
377 fputs (_("\
378 \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"),
390 stdout);
391 #if !MSDOS
392 fputs (_("\
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"),
396 stdout);
397 #endif
398 fputs (_("\
399 --backup[=CONTROL] backup before removal, choose version control\n\
400 --suffix=SUFFIX backup before removal, override usual suffix\n"),
401 stdout);
402 fputs (_("\
403 \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"),
413 stdout);
414 fputs (_("\
415 \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\
418 \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"),
422 stdout);
423 printf (_("\
424 \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);
432 }
433 exit (status);
434 }
435
436 /*----------------------------.
437 | Parse the options for tar. |
438 `----------------------------*/
439
440 /* Available option letters are DEHJQY and aejnqy. Some are reserved:
441
442 y per-file gzip compression
443 Y per-block gzip compression */
444
445 #define OPTION_STRING \
446 "-01234567ABC:F:GIK:L:MN:OPRST:UV:WX:Zb:cdf:g:hiklmoprstuvwxz"
447
448 static void
449 set_subcommand_option (enum subcommand subcommand)
450 {
451 if (subcommand_option != UNKNOWN_SUBCOMMAND
452 && subcommand_option != subcommand)
453 USAGE_ERROR ((0, 0,
454 _("You may not specify more than one `-Acdtrux' option")));
455
456 subcommand_option = subcommand;
457 }
458
459 static void
460 set_use_compress_program_option (const char *string)
461 {
462 if (use_compress_program_option && strcmp (use_compress_program_option, string) != 0)
463 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
464
465 use_compress_program_option = string;
466 }
467
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. */
471 static void
472 add_filtered_exclude (struct exclude *dummy, char const *pattern)
473 {
474 add_exclude ((strchr (pattern, '/')
475 ? excluded_with_slash
476 : excluded_without_slash),
477 pattern);
478 }
479
480 static void
481 decode_options (int argc, char *const *argv)
482 {
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;
487
488 /* Set some default option values. */
489
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);
497
498 owner_option = -1;
499 group_option = -1;
500
501 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
502
503 /* Convert old-style tar call by exploding option element and rearranging
504 options accordingly. */
505
506 if (argc > 1 && argv[1][0] != '-')
507 {
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 */
515
516 /* Initialize a constructed option. */
517
518 buffer[0] = '-';
519 buffer[2] = '\0';
520
521 /* Allocate a new argument array, and copy program name in it. */
522
523 new_argc = argc - 1 + strlen (argv[1]);
524 new_argv = xmalloc (new_argc * sizeof (char *));
525 in = argv;
526 out = new_argv;
527 *out++ = *in++;
528
529 /* Copy each old letter option as a separate option, and have the
530 corresponding argument moved next to it. */
531
532 for (letter = *in++; *letter; letter++)
533 {
534 buffer[1] = *letter;
535 *out++ = xstrdup (buffer);
536 cursor = strchr (OPTION_STRING, *letter);
537 if (cursor && cursor[1] == ':')
538 {
539 if (in < argv + argc)
540 *out++ = *in++;
541 else
542 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
543 *letter));
544 }
545 }
546
547 /* Copy all remaining options. */
548
549 while (in < argv + argc)
550 *out++ = *in++;
551
552 /* Replace the old option list by the new one. */
553
554 argc = new_argc;
555 argv = new_argv;
556 }
557
558 /* Parse all options and non-options as they appear. */
559
560 input_files = 0;
561
562 while (optchar = getopt_long (argc, argv, OPTION_STRING, long_options, 0),
563 optchar != -1)
564 switch (optchar)
565 {
566 case '?':
567 usage (TAREXIT_FAILURE);
568
569 case 0:
570 break;
571
572 case 1:
573 /* File name or non-parsed option, because of RETURN_IN_ORDER
574 ordering triggered by the leading dash in OPTION_STRING. */
575
576 name_add (optarg);
577 input_files++;
578 break;
579
580 case 'A':
581 set_subcommand_option (CAT_SUBCOMMAND);
582 break;
583
584 case OBSOLETE_BLOCK_COMPRESS:
585 WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
586 break;
587
588 case OBSOLETE_BLOCKING_FACTOR:
589 WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
590 /* Fall through. */
591
592 case 'b':
593 {
594 uintmax_t u;
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")));
600 }
601 break;
602
603 case OBSOLETE_READ_FULL_RECORDS:
604 WARN ((0, 0,
605 _("Obsolete option name replaced by --read-full-records")));
606 /* Fall through. */
607
608 case 'B':
609 /* Try to reblock input records. For reading 4.2BSD pipes. */
610
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. */
615
616 read_full_records_option = 1;
617 break;
618
619 case 'c':
620 set_subcommand_option (CREATE_SUBCOMMAND);
621 break;
622
623 case 'C':
624 name_add ("-C");
625 name_add (optarg);
626 break;
627
628 case 'd':
629 set_subcommand_option (DIFF_SUBCOMMAND);
630 break;
631
632 case 'f':
633 if (archive_names == allocated_archive_names)
634 {
635 allocated_archive_names *= 2;
636 archive_name_array =
637 xrealloc (archive_name_array,
638 sizeof (const char *) * allocated_archive_names);
639 }
640 archive_name_array[archive_names++] = optarg;
641 break;
642
643 case 'F':
644 /* Since -F is only useful with -M, make it implied. Run this
645 script at the end of each tape. */
646
647 info_script_option = optarg;
648 multi_volume_option = 1;
649 break;
650
651 case 'g':
652 listed_incremental_option = optarg;
653 after_date_option = 1;
654 /* Fall through. */
655
656 case 'G':
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. */
660
661 incremental_option = 1;
662 break;
663
664 case 'h':
665 /* Follow symbolic links. */
666
667 dereference_option = 1;
668 break;
669
670 case 'i':
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. */
674
675 ignore_zeros_option = 1;
676 break;
677
678 case 'I':
679 set_use_compress_program_option ("bzip2");
680 break;
681
682 case 'k':
683 /* Don't replace existing files. */
684 old_files_option = KEEP_OLD_FILES;
685 break;
686
687 case 'K':
688 starting_file_option = 1;
689 addname (optarg, 0);
690 break;
691
692 case 'l':
693 /* When dumping directories, don't dump files/subdirectories
694 that are on other filesystems. */
695
696 one_file_system_option = 1;
697 break;
698
699 case 'L':
700 {
701 uintmax_t u;
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;
706 }
707 break;
708
709 case OBSOLETE_TOUCH:
710 WARN ((0, 0, _("Obsolete option name replaced by --touch")));
711 /* Fall through. */
712
713 case 'm':
714 touch_option = 1;
715 break;
716
717 case 'M':
718 /* Make multivolume archive: when we can't write any more into
719 the archive, re-open it, and continue writing. */
720
721 multi_volume_option = 1;
722 break;
723
724 #if !MSDOS
725 case 'N':
726 after_date_option = 1;
727 /* Fall through. */
728
729 case NEWER_MTIME_OPTION:
730 if (newer_mtime_option != TYPE_MINIMUM (time_t))
731 USAGE_ERROR ((0, 0, _("More than one threshold date")));
732
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)));
737
738 break;
739 #endif /* not MSDOS */
740
741 case 'o':
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")));
746 break;
747
748 case 'O':
749 to_stdout_option = 1;
750 break;
751
752 case 'p':
753 same_permissions_option = 1;
754 break;
755
756 case OBSOLETE_ABSOLUTE_NAMES:
757 WARN ((0, 0, _("Obsolete option name replaced by --absolute-names")));
758 /* Fall through. */
759
760 case 'P':
761 absolute_names_option = 1;
762 break;
763
764 case 'r':
765 set_subcommand_option (APPEND_SUBCOMMAND);
766 break;
767
768 case OBSOLETE_BLOCK_NUMBER:
769 WARN ((0, 0, _("Obsolete option name replaced by --block-number")));
770 /* Fall through. */
771
772 case 'R':
773 /* Print block numbers for debugging bad tar archives. */
774
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. */
779
780 block_number_option = 1;
781 break;
782
783 case 's':
784 /* Names to extr are sorted. */
785
786 same_order_option = 1;
787 break;
788
789 case 'S':
790 sparse_option = 1;
791 break;
792
793 case 't':
794 set_subcommand_option (LIST_SUBCOMMAND);
795 verbose_option++;
796 break;
797
798 case 'T':
799 files_from_option = optarg;
800 break;
801
802 case 'u':
803 set_subcommand_option (UPDATE_SUBCOMMAND);
804 break;
805
806 case 'U':
807 old_files_option = UNLINK_FIRST_OLD_FILES;
808 break;
809
810 case 'v':
811 verbose_option++;
812 break;
813
814 case 'V':
815 volume_label_option = optarg;
816 break;
817
818 case 'w':
819 interactive_option = 1;
820 break;
821
822 case 'W':
823 verify_option = 1;
824 break;
825
826 case 'x':
827 set_subcommand_option (EXTRACT_SUBCOMMAND);
828 break;
829
830 case 'X':
831 if (add_exclude_file (add_filtered_exclude, 0, optarg, '\n') != 0)
832 {
833 int e = errno;
834 FATAL_ERROR ((0, e, "%s", quotearg_colon (optarg)));
835 }
836 break;
837
838 case 'z':
839 set_use_compress_program_option ("gzip");
840 break;
841
842 case 'Z':
843 set_use_compress_program_option ("compress");
844 break;
845
846 case OBSOLETE_VERSION_CONTROL:
847 WARN ((0, 0, _("Obsolete option name replaced by --backup")));
848 /* Fall through. */
849
850 case BACKUP_OPTION:
851 backup_option = 1;
852 if (optarg)
853 version_control_string = optarg;
854 break;
855
856 case DELETE_OPTION:
857 set_subcommand_option (DELETE_SUBCOMMAND);
858 break;
859
860 case EXCLUDE_OPTION:
861 add_filtered_exclude (0, optarg);
862 break;
863
864 case GROUP_OPTION:
865 if (! (strlen (optarg) < GNAME_FIELD_SIZE
866 && gname_to_gid (optarg, &group_option)))
867 {
868 uintmax_t g;
869 if (xstrtoumax (optarg, 0, 10, &g, "") == LONGINT_OK
870 && g == (gid_t) g)
871 group_option = g;
872 else
873 FATAL_ERROR ((0, 0, _("Invalid group given on option")));
874 }
875 break;
876
877 case MODE_OPTION:
878 mode_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")));
885 break;
886
887 case NO_RECURSE_OPTION:
888 no_recurse_option = 1;
889 break;
890
891 case NULL_OPTION:
892 filename_terminator = '\0';
893 break;
894
895 case OVERWRITE_OPTION:
896 old_files_option = OVERWRITE_OLD_FILES;
897 break;
898
899 case OWNER_OPTION:
900 if (! (strlen (optarg) < UNAME_FIELD_SIZE
901 && uname_to_uid (optarg, &owner_option)))
902 {
903 uintmax_t u;
904 if (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
905 && u == (uid_t) u)
906 owner_option = u;
907 else
908 FATAL_ERROR ((0, 0, _("Invalid owner given on option")));
909 }
910 break;
911
912 case POSIX_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")));
918 #else
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")));
923 #endif
924 break;
925
926 case PRESERVE_OPTION:
927 same_permissions_option = 1;
928 same_order_option = 1;
929 break;
930
931 case RECORD_SIZE_OPTION:
932 {
933 uintmax_t u;
934 if (! (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
935 && u == (size_t) u))
936 USAGE_ERROR ((0, 0, _("Invalid record size")));
937 record_size = u;
938 if (record_size % BLOCKSIZE != 0)
939 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
940 BLOCKSIZE));
941 blocking_factor = record_size / BLOCKSIZE;
942 }
943 break;
944
945 case RSH_COMMAND_OPTION:
946 rsh_command_option = optarg;
947 break;
948
949 case SUFFIX_OPTION:
950 backup_option = 1;
951 backup_suffix_string = optarg;
952 break;
953
954 case VOLNO_FILE_OPTION:
955 volno_file_option = optarg;
956 break;
957
958 case USE_COMPRESS_PROGRAM_OPTION:
959 set_use_compress_program_option (optarg);
960 break;
961
962 case '0':
963 case '1':
964 case '2':
965 case '3':
966 case '4':
967 case '5':
968 case '6':
969 case '7':
970
971 #ifdef DEVICE_PREFIX
972 {
973 int device = optchar - '0';
974 int density;
975 static char buf[sizeof DEVICE_PREFIX + 10];
976 char *cursor;
977
978 density = getopt_long (argc, argv, "lmh", 0, 0);
979 strcpy (buf, DEVICE_PREFIX);
980 cursor = buf + strlen (buf);
981
982 #ifdef DENSITY_LETTER
983
984 sprintf (cursor, "%d%c", device, density);
985
986 #else /* not DENSITY_LETTER */
987
988 switch (density)
989 {
990 case 'l':
991 #ifdef LOW_NUM
992 device += LOW_NUM;
993 #endif
994 break;
995
996 case 'm':
997 #ifdef MID_NUM
998 device += MID_NUM;
999 #else
1000 device += 8;
1001 #endif
1002 break;
1003
1004 case 'h':
1005 #ifdef HGH_NUM
1006 device += HGH_NUM;
1007 #else
1008 device += 16;
1009 #endif
1010 break;
1011
1012 default:
1013 usage (TAREXIT_FAILURE);
1014 }
1015 sprintf (cursor, "%d", device);
1016
1017 #endif /* not DENSITY_LETTER */
1018
1019 if (archive_names == allocated_archive_names)
1020 {
1021 allocated_archive_names *= 2;
1022 archive_name_array =
1023 xrealloc (archive_name_array,
1024 sizeof (const char *) * allocated_archive_names);
1025 }
1026 archive_name_array[archive_names++] = buf;
1027
1028 /* FIXME: How comes this works for many archives when buf is
1029 not xstrdup'ed? */
1030 }
1031 break;
1032
1033 #else /* not DEVICE_PREFIX */
1034
1035 USAGE_ERROR ((0, 0,
1036 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1037
1038 #endif /* not DEVICE_PREFIX */
1039 }
1040
1041 /* Handle operands after any "--" argument. */
1042 for (; optind < argc; optind++)
1043 {
1044 name_add (argv[optind]);
1045 input_files++;
1046 }
1047
1048 /* Process trivial options. */
1049
1050 if (show_version)
1051 {
1052 printf ("tar (GNU %s) %s\n%s\n%s\n%s\n", PACKAGE, VERSION,
1053 "Copyright 1999 Free Software Foundation, Inc.",
1054 _("\
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);
1060 }
1061
1062 if (show_help)
1063 usage (TAREXIT_SUCCESS);
1064
1065 /* Derive option values and check option consistency. */
1066
1067 if (archive_format == DEFAULT_FORMAT)
1068 {
1069 #if OLDGNU_COMPATIBILITY
1070 archive_format = OLDGNU_FORMAT;
1071 #else
1072 archive_format = GNU_FORMAT;
1073 #endif
1074 }
1075
1076 if (archive_format == GNU_FORMAT && getenv ("POSIXLY_CORRECT"))
1077 archive_format = POSIX_FORMAT;
1078
1079 if ((volume_label_option
1080 || incremental_option || multi_volume_option || sparse_option)
1081 && archive_format != OLDGNU_FORMAT && archive_format != GNU_FORMAT)
1082 USAGE_ERROR ((0, 0,
1083 _("GNU features wanted on incompatible archive format")));
1084
1085 if (archive_names == 0)
1086 {
1087 /* If no archive file name given, try TAPE from the environment, or
1088 else, DEFAULT_ARCHIVE from the configuration process. */
1089
1090 archive_names = 1;
1091 archive_name_array[0] = getenv ("TAPE");
1092 if (! archive_name_array[0])
1093 archive_name_array[0] = DEFAULT_ARCHIVE;
1094 }
1095
1096 /* Allow multiple archives only with `-M'. */
1097
1098 if (archive_names > 1 && !multi_volume_option)
1099 USAGE_ERROR ((0, 0,
1100 _("Multiple archive files requires `-M' option")));
1101
1102 if (listed_incremental_option
1103 && newer_mtime_option != TYPE_MINIMUM (time_t))
1104 USAGE_ERROR ((0, 0,
1105 _("Cannot combine --listed-incremental with --newer")));
1106
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;
1110
1111 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1112 explicit or implied, is used correctly. */
1113
1114 switch (subcommand_option)
1115 {
1116 case CREATE_SUBCOMMAND:
1117 if (input_files == 0 && !files_from_option)
1118 USAGE_ERROR ((0, 0,
1119 _("Cowardly refusing to create an empty archive")));
1120 break;
1121
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");
1130 break;
1131
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, "-"))
1139 USAGE_ERROR ((0, 0,
1140 _("Options `-Aru' are incompatible with `-f -'")));
1141
1142 default:
1143 break;
1144 }
1145
1146 archive_name_cursor = archive_name_array;
1147
1148 /* Prepare for generating backup names. */
1149
1150 if (backup_suffix_string)
1151 simple_backup_suffix = xstrdup (backup_suffix_string);
1152
1153 if (backup_option)
1154 backup_type = xget_version ("--backup", version_control_string);
1155 }
1156 \f
1157 /* Tar proper. */
1158
1159 /*-----------------------.
1160 | Main routine for tar. |
1161 `-----------------------*/
1162
1163 int
1164 main (int argc, char *const *argv)
1165 {
1166 #if HAVE_CLOCK_GETTIME
1167 if (clock_gettime (CLOCK_REALTIME, &start_timespec) != 0)
1168 #endif
1169 start_time = time (0);
1170 program_name = argv[0];
1171 setlocale (LC_ALL, "");
1172 bindtextdomain (PACKAGE, LOCALEDIR);
1173 textdomain (PACKAGE);
1174
1175 exit_status = TAREXIT_SUCCESS;
1176 filename_terminator = '\n';
1177 set_quoting_style (0, escape_quoting_style);
1178
1179 /* Pre-allocate a few structures. */
1180
1181 allocated_archive_names = 10;
1182 archive_name_array =
1183 xmalloc (sizeof (const char *) * allocated_archive_names);
1184 archive_names = 0;
1185
1186 #ifdef SIGCHLD
1187 /* System V fork+wait does not work if SIGCHLD is ignored. */
1188 signal (SIGCHLD, SIG_DFL);
1189 #endif
1190
1191 init_names ();
1192
1193 /* Decode options. */
1194
1195 decode_options (argc, argv);
1196 name_init (argc, argv);
1197
1198 /* Main command execution. */
1199
1200 if (volno_file_option)
1201 init_volume_number ();
1202
1203 switch (subcommand_option)
1204 {
1205 case UNKNOWN_SUBCOMMAND:
1206 USAGE_ERROR ((0, 0,
1207 _("You must specify one of the `-Acdtrux' options")));
1208
1209 case CAT_SUBCOMMAND:
1210 case UPDATE_SUBCOMMAND:
1211 case APPEND_SUBCOMMAND:
1212 update_archive ();
1213 break;
1214
1215 case DELETE_SUBCOMMAND:
1216 delete_archive_members ();
1217 break;
1218
1219 case CREATE_SUBCOMMAND:
1220 create_archive ();
1221 name_close ();
1222
1223 if (totals_option)
1224 print_total_written ();
1225 break;
1226
1227 case EXTRACT_SUBCOMMAND:
1228 extr_init ();
1229 read_and (extract_archive);
1230 break;
1231
1232 case LIST_SUBCOMMAND:
1233 read_and (list_archive);
1234 break;
1235
1236 case DIFF_SUBCOMMAND:
1237 diff_init ();
1238 read_and (diff_archive);
1239 break;
1240 }
1241
1242 if (volno_file_option)
1243 closeout_volume_number ();
1244
1245 /* Dispose of allocated memory, and return. */
1246
1247 free (archive_name_array);
1248 name_term ();
1249
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"));
1254 exit (exit_status);
1255 }
This page took 0.090626 seconds and 5 git commands to generate.