]> Dogcows Code - chaz/tar/blob - src/tar.c
(usage): Add examples.
[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 "xstrtol.h"
41
42 time_t get_date ();
43
44 /* Local declarations. */
45
46 #ifndef DEFAULT_ARCHIVE
47 # define DEFAULT_ARCHIVE "tar.out"
48 #endif
49
50 #ifndef DEFAULT_BLOCKING
51 # define DEFAULT_BLOCKING 20
52 #endif
53
54 static void usage PARAMS ((int));
55 \f
56 /* Miscellaneous. */
57
58 /*----------------------------------------------.
59 | Doesn't return if stdin already requested. |
60 `----------------------------------------------*/
61
62 /* Name of option using stdin. */
63 static const char *stdin_used_by;
64
65 void
66 request_stdin (const char *option)
67 {
68 if (stdin_used_by)
69 USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
70 stdin_used_by, option));
71
72 stdin_used_by = option;
73 }
74
75 /*--------------------------------------------------------.
76 | Returns true if and only if the user typed 'y' or 'Y'. |
77 `--------------------------------------------------------*/
78
79 int
80 confirm (const char *message_action, const char *message_name)
81 {
82 static FILE *confirm_file;
83 static int confirm_file_EOF;
84
85 if (!confirm_file)
86 {
87 if (archive == 0 || stdin_used_by)
88 confirm_file = fopen (TTY_NAME, "r");
89 else
90 {
91 request_stdin ("-w");
92 confirm_file = stdin;
93 }
94
95 if (!confirm_file)
96 FATAL_ERROR ((0, 0, _("Cannot read confirmation from user")));
97 }
98
99 fprintf (stdlis, "%s %s?", message_action, message_name);
100 fflush (stdlis);
101
102 {
103 int reply = confirm_file_EOF ? EOF : getc (confirm_file);
104 int character;
105
106 for (character = reply;
107 character != '\n';
108 character = getc (confirm_file))
109 if (character == EOF)
110 {
111 confirm_file_EOF = 1;
112 fputc ('\n', stdlis);
113 fflush (stdlis);
114 break;
115 }
116 return reply == 'y' || reply == 'Y';
117 }
118 }
119 \f
120 /* Options. */
121
122 /* For long options that unconditionally set a single flag, we have getopt
123 do it. For the others, we share the code for the equivalent short
124 named option, the name of which is stored in the otherwise-unused `val'
125 field of the `struct option'; for long options that have no equivalent
126 short option, we use non-characters as pseudo short options,
127 starting at CHAR_MAX + 1 and going upwards. */
128
129 enum
130 {
131 BACKUP_OPTION = CHAR_MAX + 1,
132 DELETE_OPTION,
133 EXCLUDE_OPTION,
134 GROUP_OPTION,
135 MODE_OPTION,
136 NEWER_MTIME_OPTION,
137 NO_RECURSE_OPTION,
138 NULL_OPTION,
139 OWNER_OPTION,
140 POSIX_OPTION,
141 PRESERVE_OPTION,
142 RECORD_SIZE_OPTION,
143 RSH_COMMAND_OPTION,
144 SUFFIX_OPTION,
145 USE_COMPRESS_PROGRAM_OPTION,
146 VOLNO_FILE_OPTION,
147
148 /* Some cleanup is being made in GNU tar long options. Using old names is
149 allowed for a while, but will also send a warning to stderr. Take old
150 names out in 1.14, or in summer 1997, whichever happens last. */
151
152 OBSOLETE_ABSOLUTE_NAMES,
153 OBSOLETE_BLOCK_COMPRESS,
154 OBSOLETE_BLOCKING_FACTOR,
155 OBSOLETE_BLOCK_NUMBER,
156 OBSOLETE_READ_FULL_RECORDS,
157 OBSOLETE_TOUCH,
158 OBSOLETE_VERSION_CONTROL
159 };
160
161 /* If nonzero, display usage information and exit. */
162 static int show_help;
163
164 /* If nonzero, print the version on standard output and exit. */
165 static int show_version;
166
167 struct option long_options[] =
168 {
169 {"absolute-names", no_argument, 0, 'P'},
170 {"absolute-paths", no_argument, 0, OBSOLETE_ABSOLUTE_NAMES},
171 {"after-date", required_argument, 0, 'N'},
172 {"append", no_argument, 0, 'r'},
173 {"atime-preserve", no_argument, &atime_preserve_option, 1},
174 {"backup", optional_argument, 0, BACKUP_OPTION},
175 {"block-compress", no_argument, 0, OBSOLETE_BLOCK_COMPRESS},
176 {"block-number", no_argument, 0, 'R'},
177 {"block-size", required_argument, 0, OBSOLETE_BLOCKING_FACTOR},
178 {"blocking-factor", required_argument, 0, 'b'},
179 {"bzip2", no_argument, 0, 'I'},
180 {"catenate", no_argument, 0, 'A'},
181 {"checkpoint", no_argument, &checkpoint_option, 1},
182 {"compare", no_argument, 0, 'd'},
183 {"compress", no_argument, 0, 'Z'},
184 {"concatenate", no_argument, 0, 'A'},
185 {"confirmation", no_argument, 0, 'w'},
186 /* FIXME: --selective as a synonym for --confirmation? */
187 {"create", no_argument, 0, 'c'},
188 {"delete", no_argument, 0, DELETE_OPTION},
189 {"dereference", no_argument, 0, 'h'},
190 {"diff", no_argument, 0, 'd'},
191 {"directory", required_argument, 0, 'C'},
192 {"exclude", required_argument, 0, EXCLUDE_OPTION},
193 {"exclude-from", required_argument, 0, 'X'},
194 {"extract", no_argument, 0, 'x'},
195 {"file", required_argument, 0, 'f'},
196 {"files-from", required_argument, 0, 'T'},
197 {"force-local", no_argument, &force_local_option, 1},
198 {"get", no_argument, 0, 'x'},
199 {"group", required_argument, 0, GROUP_OPTION},
200 {"gunzip", no_argument, 0, 'z'},
201 {"gzip", no_argument, 0, 'z'},
202 {"help", no_argument, &show_help, 1},
203 {"ignore-failed-read", no_argument, &ignore_failed_read_option, 1},
204 {"ignore-zeros", no_argument, 0, 'i'},
205 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
206 {"incremental", no_argument, 0, 'G'},
207 {"info-script", required_argument, 0, 'F'},
208 {"interactive", no_argument, 0, 'w'},
209 {"keep-old-files", no_argument, 0, 'k'},
210 {"label", required_argument, 0, 'V'},
211 {"list", no_argument, 0, 't'},
212 {"listed-incremental", required_argument, 0, 'g'},
213 {"mode", required_argument, 0, MODE_OPTION},
214 {"modification-time", no_argument, 0, OBSOLETE_TOUCH},
215 {"multi-volume", no_argument, 0, 'M'},
216 {"new-volume-script", required_argument, 0, 'F'},
217 {"newer", required_argument, 0, 'N'},
218 {"newer-mtime", required_argument, 0, NEWER_MTIME_OPTION},
219 {"null", no_argument, 0, NULL_OPTION},
220 {"no-recursion", no_argument, 0, NO_RECURSE_OPTION},
221 {"no-same-owner", no_argument, &same_owner_option, -1},
222 {"no-same-permissions", no_argument, &same_permissions_option, -1},
223 {"numeric-owner", no_argument, &numeric_owner_option, 1},
224 {"old-archive", no_argument, 0, 'o'},
225 {"one-file-system", no_argument, 0, 'l'},
226 {"owner", required_argument, 0, OWNER_OPTION},
227 {"portability", no_argument, 0, 'o'},
228 {"posix", no_argument, 0, POSIX_OPTION},
229 {"preserve", no_argument, 0, PRESERVE_OPTION},
230 {"preserve-order", no_argument, 0, 's'},
231 {"preserve-permissions", no_argument, 0, 'p'},
232 {"recursive-unlink", no_argument, &recursive_unlink_option, 1},
233 {"read-full-blocks", no_argument, 0, OBSOLETE_READ_FULL_RECORDS},
234 {"read-full-records", no_argument, 0, 'B'},
235 /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
236 {"record-number", no_argument, 0, OBSOLETE_BLOCK_NUMBER},
237 {"record-size", required_argument, 0, RECORD_SIZE_OPTION},
238 {"remove-files", no_argument, &remove_files_option, 1},
239 {"rsh-command", required_argument, 0, RSH_COMMAND_OPTION},
240 {"same-order", no_argument, 0, 's'},
241 {"same-owner", no_argument, &same_owner_option, 1},
242 {"same-permissions", no_argument, 0, 'p'},
243 {"show-omitted-dirs", no_argument, &show_omitted_dirs_option, 1},
244 {"sparse", no_argument, 0, 'S'},
245 {"starting-file", required_argument, 0, 'K'},
246 {"suffix", required_argument, 0, SUFFIX_OPTION},
247 {"tape-length", required_argument, 0, 'L'},
248 {"to-stdout", no_argument, 0, 'O'},
249 {"totals", no_argument, &totals_option, 1},
250 {"touch", no_argument, 0, 'm'},
251 {"uncompress", no_argument, 0, 'Z'},
252 {"ungzip", no_argument, 0, 'z'},
253 {"unlink-first", no_argument, 0, 'U'},
254 {"update", no_argument, 0, 'u'},
255 {"use-compress-program", required_argument, 0, USE_COMPRESS_PROGRAM_OPTION},
256 {"verbose", no_argument, 0, 'v'},
257 {"verify", no_argument, 0, 'W'},
258 {"version", no_argument, &show_version, 1},
259 {"version-control", required_argument, 0, OBSOLETE_VERSION_CONTROL},
260 {"volno-file", required_argument, 0, VOLNO_FILE_OPTION},
261
262 {0, 0, 0, 0}
263 };
264
265 /*---------------------------------------------.
266 | Print a usage message and exit with STATUS. |
267 `---------------------------------------------*/
268
269 static void
270 usage (int status)
271 {
272 if (status != TAREXIT_SUCCESS)
273 fprintf (stderr, _("Try `%s --help' for more information.\n"),
274 program_name);
275 else
276 {
277 fputs (_("\
278 GNU `tar' saves many files together into a single tape or disk archive, and\n\
279 can restore individual files from the archive.\n"),
280 stdout);
281 printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
282 \n\
283 Examples:\n\
284 %s -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
285 %s -tvf archive.tar # List all files in archive.tar verbosely.\n\
286 %s -xf archive.tar # Extract all files from archive.tar.\n"),
287 program_name, program_name, program_name, program_name);
288 fputs (_("\
289 \n\
290 If a long option shows an argument as mandatory, then it is mandatory\n\
291 for the equivalent short option also. Similarly for optional arguments.\n"),
292 stdout);
293 fputs(_("\
294 \n\
295 Main operation mode:\n\
296 -t, --list list the contents of an archive\n\
297 -x, --extract, --get extract files from an archive\n\
298 -c, --create create a new archive\n\
299 -d, --diff, --compare find differences between archive and file system\n\
300 -r, --append append files to the end of an archive\n\
301 -u, --update only append files newer than copy in archive\n\
302 -A, --catenate append tar files to an archive\n\
303 --concatenate same as -A\n\
304 --delete delete from the archive (not on mag tapes!)\n"),
305 stdout);
306 fputs (_("\
307 \n\
308 Operation modifiers:\n\
309 -W, --verify attempt to verify the archive after writing it\n\
310 --remove-files remove files after adding them to the archive\n\
311 -k, --keep-old-files don't overwrite existing files when extracting\n\
312 -U, --unlink-first remove each file prior to extracting over it\n\
313 --recursive-unlink empty hierarchies prior to extracting directory\n\
314 -S, --sparse handle sparse files efficiently\n\
315 -O, --to-stdout extract files to standard output\n\
316 -G, --incremental handle old GNU-format incremental backup\n\
317 -g, --listed-incremental=FILE\n\
318 handle new GNU-format incremental backup\n\
319 --ignore-failed-read do not exit with nonzero on unreadable files\n"),
320 stdout);
321 fputs (_("\
322 \n\
323 Handling of file attributes:\n\
324 --owner=NAME force NAME as owner for added files\n\
325 --group=NAME force NAME as group for added files\n\
326 --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
327 --atime-preserve don't change access times on dumped files\n\
328 -m, --modification-time don't extract file modified time\n\
329 --same-owner try extracting files with the same ownership\n\
330 --no-same-owner extract files as yourself\n\
331 --numeric-owner always use numbers for user/group names\n\
332 -p, --same-permissions extract permissions information\n\
333 --no-same-permissions do not extract permissions information\n\
334 --preserve-permissions same as -p\n\
335 -s, --same-order sort names to extract to match archive\n\
336 --preserve-order same as -s\n\
337 --preserve same as both -p and -s\n"),
338 stdout);
339 fputs (_("\
340 \n\
341 Device selection and switching:\n\
342 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
343 --force-local archive file is local even if has a colon\n\
344 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
345 -[0-7][lmh] specify drive and density\n\
346 -M, --multi-volume create/list/extract multi-volume archive\n\
347 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
348 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
349 --new-volume-script=FILE same as -F FILE\n\
350 --volno-file=FILE use/update the volume number in FILE\n"),
351 stdout);
352 fputs (_("\
353 \n\
354 Device blocking:\n\
355 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
356 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
357 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
358 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
359 stdout);
360 fputs (_("\
361 \n\
362 Archive format selection:\n\
363 -V, --label=NAME create archive with volume name NAME\n\
364 PATTERN at list/extract time, a globbing PATTERN\n\
365 -o, --old-archive, --portability write a V7 format archive\n\
366 --posix write a POSIX format archive\n\
367 -I, --bzip2 filter the archive through bzip2\n\
368 -z, --gzip, --ungzip filter the archive through gzip\n\
369 -Z, --compress, --uncompress filter the archive through compress\n\
370 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
371 stdout);
372 fputs (_("\
373 \n\
374 Local file selection:\n\
375 -C, --directory=DIR change to directory DIR\n\
376 -T, --files-from=NAME get names to extract or create from file NAME\n\
377 --null -T reads null-terminated names, disable -C\n\
378 --exclude=PATTERN exclude files, given as a globbing PATTERN\n\
379 -X, --exclude-from=FILE exclude globbing patterns listed in FILE\n\
380 -P, --absolute-names don't strip leading `/'s from file names\n\
381 -h, --dereference dump instead the files symlinks point to\n\
382 --no-recursion avoid descending automatically in directories\n\
383 -l, --one-file-system stay in local file system when creating archive\n\
384 -K, --starting-file=NAME begin at file NAME in the archive\n"),
385 stdout);
386 #if !MSDOS
387 fputs (_("\
388 -N, --newer=DATE only store files newer than DATE\n\
389 --newer-mtime compare date and time when data changed only\n\
390 --after-date=DATE same as -N\n"),
391 stdout);
392 #endif
393 fputs (_("\
394 --backup[=CONTROL] backup before removal, choose version control\n\
395 --suffix=SUFFIX backup before removal, override usual suffix\n"),
396 stdout);
397 fputs (_("\
398 \n\
399 Informative output:\n\
400 --help print this help, then exit\n\
401 --version print tar program version number, then exit\n\
402 -v, --verbose verbosely list files processed\n\
403 --checkpoint print directory names while reading the archive\n\
404 --totals print total bytes written while creating archive\n\
405 -R, --block-number show block number within archive with each message\n\
406 -w, --interactive ask for confirmation for every action\n\
407 --confirmation same as -w\n"),
408 stdout);
409 fputs (_("\
410 \n\
411 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
412 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
413 \n\
414 t, numbered make numbered backups\n\
415 nil, existing numbered if numbered backups exist, simple otherwise\n\
416 never, simple always make simple backups\n"),
417 stdout);
418 printf (_("\
419 \n\
420 GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
421 is set in the environment, GNU extensions are disallowed with `--posix'.\n\
422 Support for POSIX is only partially implemented, don't count on it yet.\n\
423 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; and FILE may be a file\n\
424 or a device. *This* `tar' defaults to `-f%s -b%d'.\n"),
425 DEFAULT_ARCHIVE, DEFAULT_BLOCKING);
426 fputs (_("\nReport bugs to <bug-tar@gnu.org>.\n"), stdout);
427 }
428 exit (status);
429 }
430
431 /*----------------------------.
432 | Parse the options for tar. |
433 `----------------------------*/
434
435 /* Available option letters are DEHJQY and aejnqy. Some are reserved:
436
437 y per-file gzip compression
438 Y per-block gzip compression */
439
440 #define OPTION_STRING \
441 "-01234567ABC:F:GIK:L:MN:OPRST:UV:WX:Zb:cdf:g:hiklmoprstuvwxz"
442
443 static void
444 set_subcommand_option (enum subcommand subcommand)
445 {
446 if (subcommand_option != UNKNOWN_SUBCOMMAND
447 && subcommand_option != subcommand)
448 USAGE_ERROR ((0, 0,
449 _("You may not specify more than one `-Acdtrux' option")));
450
451 subcommand_option = subcommand;
452 }
453
454 static void
455 set_use_compress_program_option (const char *string)
456 {
457 if (use_compress_program_option && strcmp (use_compress_program_option, string) != 0)
458 USAGE_ERROR ((0, 0, _("Conflicting compression options")));
459
460 use_compress_program_option = string;
461 }
462
463 /* Ignore DUMMY (which will always be null in practice), and add
464 PATTERN to the proper set of patterns to be excluded -- either
465 patterns with slashes, or patterns without. */
466 static void
467 add_filtered_exclude (struct exclude *dummy, char const *pattern)
468 {
469 add_exclude ((strchr (pattern, '/')
470 ? excluded_with_slash
471 : excluded_without_slash),
472 pattern);
473 }
474
475 static void
476 decode_options (int argc, char *const *argv)
477 {
478 int optchar; /* option letter */
479 int input_files; /* number of input files */
480 const char *backup_suffix_string;
481 const char *version_control_string = 0;
482
483 /* Set some default option values. */
484
485 subcommand_option = UNKNOWN_SUBCOMMAND;
486 archive_format = DEFAULT_FORMAT;
487 blocking_factor = DEFAULT_BLOCKING;
488 record_size = DEFAULT_BLOCKING * BLOCKSIZE;
489 excluded_with_slash = new_exclude ();
490 excluded_without_slash = new_exclude ();
491 newer_mtime_option = TYPE_MINIMUM (time_t);
492
493 owner_option = -1;
494 group_option = -1;
495
496 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
497
498 /* Convert old-style tar call by exploding option element and rearranging
499 options accordingly. */
500
501 if (argc > 1 && argv[1][0] != '-')
502 {
503 int new_argc; /* argc value for rearranged arguments */
504 char **new_argv; /* argv value for rearranged arguments */
505 char *const *in; /* cursor into original argv */
506 char **out; /* cursor into rearranged argv */
507 const char *letter; /* cursor into old option letters */
508 char buffer[3]; /* constructed option buffer */
509 const char *cursor; /* cursor in OPTION_STRING */
510
511 /* Initialize a constructed option. */
512
513 buffer[0] = '-';
514 buffer[2] = '\0';
515
516 /* Allocate a new argument array, and copy program name in it. */
517
518 new_argc = argc - 1 + strlen (argv[1]);
519 new_argv = xmalloc (new_argc * sizeof (char *));
520 in = argv;
521 out = new_argv;
522 *out++ = *in++;
523
524 /* Copy each old letter option as a separate option, and have the
525 corresponding argument moved next to it. */
526
527 for (letter = *in++; *letter; letter++)
528 {
529 buffer[1] = *letter;
530 *out++ = xstrdup (buffer);
531 cursor = strchr (OPTION_STRING, *letter);
532 if (cursor && cursor[1] == ':')
533 {
534 if (in < argv + argc)
535 *out++ = *in++;
536 else
537 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
538 *letter));
539 }
540 }
541
542 /* Copy all remaining options. */
543
544 while (in < argv + argc)
545 *out++ = *in++;
546
547 /* Replace the old option list by the new one. */
548
549 argc = new_argc;
550 argv = new_argv;
551 }
552
553 /* Parse all options and non-options as they appear. */
554
555 input_files = 0;
556
557 while (optchar = getopt_long (argc, argv, OPTION_STRING, long_options, 0),
558 optchar != -1)
559 switch (optchar)
560 {
561 case '?':
562 usage (TAREXIT_FAILURE);
563
564 case 0:
565 break;
566
567 case 1:
568 /* File name or non-parsed option, because of RETURN_IN_ORDER
569 ordering triggered by the leading dash in OPTION_STRING. */
570
571 name_add (optarg);
572 input_files++;
573 break;
574
575 case 'A':
576 set_subcommand_option (CAT_SUBCOMMAND);
577 break;
578
579 case OBSOLETE_BLOCK_COMPRESS:
580 WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
581 break;
582
583 case OBSOLETE_BLOCKING_FACTOR:
584 WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
585 /* Fall through. */
586
587 case 'b':
588 {
589 uintmax_t u;
590 if (! (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
591 && u == (blocking_factor = u)
592 && 0 < blocking_factor
593 && u == (record_size = u * BLOCKSIZE) / BLOCKSIZE))
594 USAGE_ERROR ((0, 0, _("Invalid blocking factor")));
595 }
596 break;
597
598 case OBSOLETE_READ_FULL_RECORDS:
599 WARN ((0, 0,
600 _("Obsolete option name replaced by --read-full-records")));
601 /* Fall through. */
602
603 case 'B':
604 /* Try to reblock input records. For reading 4.2BSD pipes. */
605
606 /* It would surely make sense to exchange -B and -R, but it seems
607 that -B has been used for a long while in Sun tar ans most
608 BSD-derived systems. This is a consequence of the block/record
609 terminology confusion. */
610
611 read_full_records_option = 1;
612 break;
613
614 case 'c':
615 set_subcommand_option (CREATE_SUBCOMMAND);
616 break;
617
618 case 'C':
619 name_add ("-C");
620 name_add (optarg);
621 break;
622
623 case 'd':
624 set_subcommand_option (DIFF_SUBCOMMAND);
625 break;
626
627 case 'f':
628 if (archive_names == allocated_archive_names)
629 {
630 allocated_archive_names *= 2;
631 archive_name_array =
632 xrealloc (archive_name_array,
633 sizeof (const char *) * allocated_archive_names);
634 }
635 archive_name_array[archive_names++] = optarg;
636 break;
637
638 case 'F':
639 /* Since -F is only useful with -M, make it implied. Run this
640 script at the end of each tape. */
641
642 info_script_option = optarg;
643 multi_volume_option = 1;
644 break;
645
646 case 'g':
647 listed_incremental_option = optarg;
648 after_date_option = 1;
649 /* Fall through. */
650
651 case 'G':
652 /* We are making an incremental dump (FIXME: are we?); save
653 directories at the beginning of the archive, and include in each
654 directory its contents. */
655
656 incremental_option = 1;
657 break;
658
659 case 'h':
660 /* Follow symbolic links. */
661
662 dereference_option = 1;
663 break;
664
665 case 'i':
666 /* Ignore zero blocks (eofs). This can't be the default,
667 because Unix tar writes two blocks of zeros, then pads out
668 the record with garbage. */
669
670 ignore_zeros_option = 1;
671 break;
672
673 case 'I':
674 set_use_compress_program_option ("bzip2");
675 break;
676
677 case 'k':
678 /* Don't overwrite existing files. */
679
680 keep_old_files_option = 1;
681 break;
682
683 case 'K':
684 starting_file_option = 1;
685 addname (optarg, 0);
686 break;
687
688 case 'l':
689 /* When dumping directories, don't dump files/subdirectories
690 that are on other filesystems. */
691
692 one_file_system_option = 1;
693 break;
694
695 case 'L':
696 {
697 uintmax_t u;
698 if (xstrtoumax (optarg, 0, 10, &u, "") != LONGINT_OK)
699 USAGE_ERROR ((0, 0, _("Invalid tape length")));
700 tape_length_option = 1024 * (tarlong) u;
701 multi_volume_option = 1;
702 }
703 break;
704
705 case OBSOLETE_TOUCH:
706 WARN ((0, 0, _("Obsolete option name replaced by --touch")));
707 /* Fall through. */
708
709 case 'm':
710 touch_option = 1;
711 break;
712
713 case 'M':
714 /* Make multivolume archive: when we can't write any more into
715 the archive, re-open it, and continue writing. */
716
717 multi_volume_option = 1;
718 break;
719
720 #if !MSDOS
721 case 'N':
722 after_date_option = 1;
723 /* Fall through. */
724
725 case NEWER_MTIME_OPTION:
726 if (newer_mtime_option != TYPE_MINIMUM (time_t))
727 USAGE_ERROR ((0, 0, _("More than one threshold date")));
728
729 newer_mtime_option = get_date (optarg, 0);
730 if (newer_mtime_option == (time_t) -1)
731 WARN ((0, 0, _("Substituting %s for unknown date format `%s'"),
732 tartime (newer_mtime_option), optarg));
733
734 break;
735 #endif /* not MSDOS */
736
737 case 'o':
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")));
742 break;
743
744 case 'O':
745 to_stdout_option = 1;
746 break;
747
748 case 'p':
749 same_permissions_option = 1;
750 break;
751
752 case OBSOLETE_ABSOLUTE_NAMES:
753 WARN ((0, 0, _("Obsolete option name replaced by --absolute-names")));
754 /* Fall through. */
755
756 case 'P':
757 absolute_names_option = 1;
758 break;
759
760 case 'r':
761 set_subcommand_option (APPEND_SUBCOMMAND);
762 break;
763
764 case OBSOLETE_BLOCK_NUMBER:
765 WARN ((0, 0, _("Obsolete option name replaced by --block-number")));
766 /* Fall through. */
767
768 case 'R':
769 /* Print block numbers for debugging bad tar archives. */
770
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. */
775
776 block_number_option = 1;
777 break;
778
779 case 's':
780 /* Names to extr are sorted. */
781
782 same_order_option = 1;
783 break;
784
785 case 'S':
786 sparse_option = 1;
787 break;
788
789 case 't':
790 set_subcommand_option (LIST_SUBCOMMAND);
791 verbose_option++;
792 break;
793
794 case 'T':
795 files_from_option = optarg;
796 break;
797
798 case 'u':
799 set_subcommand_option (UPDATE_SUBCOMMAND);
800 break;
801
802 case 'U':
803 unlink_first_option = 1;
804 break;
805
806 case 'v':
807 verbose_option++;
808 break;
809
810 case 'V':
811 volume_label_option = optarg;
812 break;
813
814 case 'w':
815 interactive_option = 1;
816 break;
817
818 case 'W':
819 verify_option = 1;
820 break;
821
822 case 'x':
823 set_subcommand_option (EXTRACT_SUBCOMMAND);
824 break;
825
826 case 'X':
827 if (add_exclude_file (add_filtered_exclude, 0, optarg, '\n') != 0)
828 FATAL_ERROR ((0, errno, "%s", optarg));
829 break;
830
831 case 'z':
832 set_use_compress_program_option ("gzip");
833 break;
834
835 case 'Z':
836 set_use_compress_program_option ("compress");
837 break;
838
839 case OBSOLETE_VERSION_CONTROL:
840 WARN ((0, 0, _("Obsolete option name replaced by --backup")));
841 /* Fall through. */
842
843 case BACKUP_OPTION:
844 backup_option = 1;
845 if (optarg)
846 version_control_string = optarg;
847 break;
848
849 case DELETE_OPTION:
850 set_subcommand_option (DELETE_SUBCOMMAND);
851 break;
852
853 case EXCLUDE_OPTION:
854 add_filtered_exclude (0, optarg);
855 break;
856
857 case GROUP_OPTION:
858 if (! (strlen (optarg) < GNAME_FIELD_SIZE
859 && gname_to_gid (optarg, &group_option)))
860 {
861 uintmax_t g;
862 if (xstrtoumax (optarg, 0, 10, &g, "") == LONGINT_OK
863 && g == (gid_t) g)
864 group_option = g;
865 else
866 FATAL_ERROR ((0, 0, _("Invalid group given on option")));
867 }
868 break;
869
870 case MODE_OPTION:
871 mode_option
872 = mode_compile (optarg,
873 MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
874 if (mode_option == MODE_INVALID)
875 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
876 if (mode_option == MODE_MEMORY_EXHAUSTED)
877 FATAL_ERROR ((0, 0, _("Memory exhausted")));
878 break;
879
880 case NO_RECURSE_OPTION:
881 no_recurse_option = 1;
882 break;
883
884 case NULL_OPTION:
885 filename_terminator = '\0';
886 break;
887
888 case OWNER_OPTION:
889 if (! (strlen (optarg) < UNAME_FIELD_SIZE
890 && uname_to_uid (optarg, &owner_option)))
891 {
892 uintmax_t u;
893 if (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
894 && u == (uid_t) u)
895 owner_option = u;
896 else
897 FATAL_ERROR ((0, 0, _("Invalid owner given on option")));
898 }
899 break;
900
901 case POSIX_OPTION:
902 #if OLDGNU_COMPATIBILITY
903 if (archive_format == DEFAULT_FORMAT)
904 archive_format = GNU_FORMAT;
905 else if (archive_format != GNU_FORMAT)
906 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
907 #else
908 if (archive_format == DEFAULT_FORMAT)
909 archive_format = POSIX_FORMAT;
910 else if (archive_format != POSIX_FORMAT)
911 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
912 #endif
913 break;
914
915 case PRESERVE_OPTION:
916 same_permissions_option = 1;
917 same_order_option = 1;
918 break;
919
920 case RECORD_SIZE_OPTION:
921 {
922 uintmax_t u;
923 if (! (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
924 && u == (size_t) u))
925 USAGE_ERROR ((0, 0, _("Invalid record size")));
926 record_size = u;
927 if (record_size % BLOCKSIZE != 0)
928 USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
929 BLOCKSIZE));
930 blocking_factor = record_size / BLOCKSIZE;
931 }
932 break;
933
934 case RSH_COMMAND_OPTION:
935 rsh_command_option = optarg;
936 break;
937
938 case SUFFIX_OPTION:
939 backup_option = 1;
940 backup_suffix_string = optarg;
941 break;
942
943 case VOLNO_FILE_OPTION:
944 volno_file_option = optarg;
945 break;
946
947 case USE_COMPRESS_PROGRAM_OPTION:
948 set_use_compress_program_option (optarg);
949 break;
950
951 case '0':
952 case '1':
953 case '2':
954 case '3':
955 case '4':
956 case '5':
957 case '6':
958 case '7':
959
960 #ifdef DEVICE_PREFIX
961 {
962 int device = optchar - '0';
963 int density;
964 static char buf[sizeof DEVICE_PREFIX + 10];
965 char *cursor;
966
967 density = getopt_long (argc, argv, "lmh", 0, 0);
968 strcpy (buf, DEVICE_PREFIX);
969 cursor = buf + strlen (buf);
970
971 #ifdef DENSITY_LETTER
972
973 sprintf (cursor, "%d%c", device, density);
974
975 #else /* not DENSITY_LETTER */
976
977 switch (density)
978 {
979 case 'l':
980 #ifdef LOW_NUM
981 device += LOW_NUM;
982 #endif
983 break;
984
985 case 'm':
986 #ifdef MID_NUM
987 device += MID_NUM;
988 #else
989 device += 8;
990 #endif
991 break;
992
993 case 'h':
994 #ifdef HGH_NUM
995 device += HGH_NUM;
996 #else
997 device += 16;
998 #endif
999 break;
1000
1001 default:
1002 usage (TAREXIT_FAILURE);
1003 }
1004 sprintf (cursor, "%d", device);
1005
1006 #endif /* not DENSITY_LETTER */
1007
1008 if (archive_names == allocated_archive_names)
1009 {
1010 allocated_archive_names *= 2;
1011 archive_name_array =
1012 xrealloc (archive_name_array,
1013 sizeof (const char *) * allocated_archive_names);
1014 }
1015 archive_name_array[archive_names++] = buf;
1016
1017 /* FIXME: How comes this works for many archives when buf is
1018 not xstrdup'ed? */
1019 }
1020 break;
1021
1022 #else /* not DEVICE_PREFIX */
1023
1024 USAGE_ERROR ((0, 0,
1025 _("Options `-[0-7][lmh]' not supported by *this* tar")));
1026
1027 #endif /* not DEVICE_PREFIX */
1028 }
1029
1030 /* Handle operands after any "--" argument. */
1031 for (; optind < argc; optind++)
1032 {
1033 name_add (argv[optind]);
1034 input_files++;
1035 }
1036
1037 /* Process trivial options. */
1038
1039 if (show_version)
1040 {
1041 printf ("tar (GNU %s) %s\n%s\n%s\n%s\n", PACKAGE, VERSION,
1042 "Copyright 1999 Free Software Foundation, Inc.",
1043 _("\
1044 This program comes with NO WARRANTY, to the extent permitted by law.\n\
1045 You may redistribute it under the terms of the GNU General Public License;\n\
1046 see the file named COPYING for details."),
1047 _("Written by John Gilmore and Jay Fenlason."));
1048 exit (TAREXIT_SUCCESS);
1049 }
1050
1051 if (show_help)
1052 usage (TAREXIT_SUCCESS);
1053
1054 /* Derive option values and check option consistency. */
1055
1056 if (archive_format == DEFAULT_FORMAT)
1057 {
1058 #if OLDGNU_COMPATIBILITY
1059 archive_format = OLDGNU_FORMAT;
1060 #else
1061 archive_format = GNU_FORMAT;
1062 #endif
1063 }
1064
1065 if (archive_format == GNU_FORMAT && getenv ("POSIXLY_CORRECT"))
1066 archive_format = POSIX_FORMAT;
1067
1068 if ((volume_label_option
1069 || incremental_option || multi_volume_option || sparse_option)
1070 && archive_format != OLDGNU_FORMAT && archive_format != GNU_FORMAT)
1071 USAGE_ERROR ((0, 0,
1072 _("GNU features wanted on incompatible archive format")));
1073
1074 if (archive_names == 0)
1075 {
1076 /* If no archive file name given, try TAPE from the environment, or
1077 else, DEFAULT_ARCHIVE from the configuration process. */
1078
1079 archive_names = 1;
1080 archive_name_array[0] = getenv ("TAPE");
1081 if (! archive_name_array[0])
1082 archive_name_array[0] = DEFAULT_ARCHIVE;
1083 }
1084
1085 /* Allow multiple archives only with `-M'. */
1086
1087 if (archive_names > 1 && !multi_volume_option)
1088 USAGE_ERROR ((0, 0,
1089 _("Multiple archive files requires `-M' option")));
1090
1091 if (listed_incremental_option
1092 && newer_mtime_option != TYPE_MINIMUM (time_t))
1093 USAGE_ERROR ((0, 0,
1094 _("Cannot combine --listed-incremental with --newer")));
1095
1096 /* If ready to unlink hierarchies, so we are for simpler files. */
1097 if (recursive_unlink_option)
1098 unlink_first_option = 1;
1099
1100 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1101 explicit or implied, is used correctly. */
1102
1103 switch (subcommand_option)
1104 {
1105 case CREATE_SUBCOMMAND:
1106 if (input_files == 0 && !files_from_option)
1107 USAGE_ERROR ((0, 0,
1108 _("Cowardly refusing to create an empty archive")));
1109 break;
1110
1111 case EXTRACT_SUBCOMMAND:
1112 case LIST_SUBCOMMAND:
1113 case DIFF_SUBCOMMAND:
1114 for (archive_name_cursor = archive_name_array;
1115 archive_name_cursor < archive_name_array + archive_names;
1116 archive_name_cursor++)
1117 if (!strcmp (*archive_name_cursor, "-"))
1118 request_stdin ("-f");
1119 break;
1120
1121 case CAT_SUBCOMMAND:
1122 case UPDATE_SUBCOMMAND:
1123 case APPEND_SUBCOMMAND:
1124 for (archive_name_cursor = archive_name_array;
1125 archive_name_cursor < archive_name_array + archive_names;
1126 archive_name_cursor++)
1127 if (!strcmp (*archive_name_cursor, "-"))
1128 USAGE_ERROR ((0, 0,
1129 _("Options `-Aru' are incompatible with `-f -'")));
1130
1131 default:
1132 break;
1133 }
1134
1135 archive_name_cursor = archive_name_array;
1136
1137 /* Prepare for generating backup names. */
1138
1139 if (backup_suffix_string)
1140 simple_backup_suffix = xstrdup (backup_suffix_string);
1141
1142 if (backup_option)
1143 backup_type = xget_version ("--backup", version_control_string);
1144 }
1145 \f
1146 /* Tar proper. */
1147
1148 /*-----------------------.
1149 | Main routine for tar. |
1150 `-----------------------*/
1151
1152 int
1153 main (int argc, char *const *argv)
1154 {
1155 #if HAVE_CLOCK_GETTIME
1156 if (clock_gettime (CLOCK_REALTIME, &start_timespec) != 0)
1157 #endif
1158 start_time = time (0);
1159 program_name = argv[0];
1160 setlocale (LC_ALL, "");
1161 bindtextdomain (PACKAGE, LOCALEDIR);
1162 textdomain (PACKAGE);
1163
1164 exit_status = TAREXIT_SUCCESS;
1165 filename_terminator = '\n';
1166
1167 /* Pre-allocate a few structures. */
1168
1169 allocated_archive_names = 10;
1170 archive_name_array =
1171 xmalloc (sizeof (const char *) * allocated_archive_names);
1172 archive_names = 0;
1173
1174 #ifdef SIGCHLD
1175 /* System V fork+wait does not work if SIGCHLD is ignored. */
1176 signal (SIGCHLD, SIG_DFL);
1177 #endif
1178
1179 init_names ();
1180
1181 /* Decode options. */
1182
1183 decode_options (argc, argv);
1184 name_init (argc, argv);
1185
1186 /* Main command execution. */
1187
1188 if (volno_file_option)
1189 init_volume_number ();
1190
1191 switch (subcommand_option)
1192 {
1193 case UNKNOWN_SUBCOMMAND:
1194 USAGE_ERROR ((0, 0,
1195 _("You must specify one of the `-Acdtrux' options")));
1196
1197 case CAT_SUBCOMMAND:
1198 case UPDATE_SUBCOMMAND:
1199 case APPEND_SUBCOMMAND:
1200 update_archive ();
1201 break;
1202
1203 case DELETE_SUBCOMMAND:
1204 delete_archive_members ();
1205 break;
1206
1207 case CREATE_SUBCOMMAND:
1208 create_archive ();
1209 name_close ();
1210
1211 if (totals_option)
1212 print_total_written ();
1213 break;
1214
1215 case EXTRACT_SUBCOMMAND:
1216 extr_init ();
1217 read_and (extract_archive);
1218 break;
1219
1220 case LIST_SUBCOMMAND:
1221 read_and (list_archive);
1222 break;
1223
1224 case DIFF_SUBCOMMAND:
1225 diff_init ();
1226 read_and (diff_archive);
1227 break;
1228 }
1229
1230 if (volno_file_option)
1231 closeout_volume_number ();
1232
1233 /* Dispose of allocated memory, and return. */
1234
1235 free (archive_name_array);
1236 name_term ();
1237
1238 if (stdlis == stdout && (ferror (stdout) || fclose (stdout) != 0))
1239 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1240 if (exit_status == TAREXIT_FAILURE)
1241 error (0, 0, _("Error exit delayed from previous errors"));
1242 exit (exit_status);
1243 }
This page took 0.088908 seconds and 5 git commands to generate.