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