]> Dogcows Code - chaz/tar/blob - src/tar.c
Include fnmatch.h.
[chaz/tar] / src / tar.c
1 /* A tar (tape archiver) program.
2 Copyright 1988, 92,93,94,95,96,97,99, 2000 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 <fnmatch.h>
22 #include <getopt.h>
23
24 #include <signal.h>
25 #if ! defined SIGCHLD && defined SIGCLD
26 # define SIGCHLD SIGCLD
27 #endif
28
29 #include <time.h>
30 #ifndef time
31 time_t time ();
32 #endif
33
34 /* The following causes "common.h" to produce definitions of all the global
35 variables, rather than just "extern" declarations of them. GNU tar does
36 depend on the system loader to preset all GLOBAL variables to neutral (or
37 zero) values; explicit initialization is usually not done. */
38 #define GLOBAL
39 #include "common.h"
40
41 #include <localedir.h>
42 #include <prepargs.h>
43 #include <quotearg.h>
44 #include <xstrtol.h>
45
46 time_t get_date ();
47
48 /* Local declarations. */
49
50 #ifndef DEFAULT_ARCHIVE
51 # define DEFAULT_ARCHIVE "tar.out"
52 #endif
53
54 #ifndef DEFAULT_BLOCKING
55 # define DEFAULT_BLOCKING 20
56 #endif
57
58 static void usage PARAMS ((int)) __attribute__ ((noreturn));
59 \f
60 /* Miscellaneous. */
61
62 /* Name of option using stdin. */
63 static const char *stdin_used_by;
64
65 /* Doesn't return if stdin already requested. */
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 /* Returns true if and only if the user typed 'y' or 'Y'. */
77 int
78 confirm (const char *message_action, const char *message_name)
79 {
80 static FILE *confirm_file;
81 static int confirm_file_EOF;
82
83 if (!confirm_file)
84 {
85 if (archive == 0 || stdin_used_by)
86 {
87 confirm_file = fopen (TTY_NAME, "r");
88 if (! confirm_file)
89 open_fatal (TTY_NAME);
90 }
91 else
92 {
93 request_stdin ("-w");
94 confirm_file = stdin;
95 }
96 }
97
98 fprintf (stdlis, "%s %s?", message_action, quote (message_name));
99 fflush (stdlis);
100
101 {
102 int reply = confirm_file_EOF ? EOF : getc (confirm_file);
103 int character;
104
105 for (character = reply;
106 character != '\n';
107 character = getc (confirm_file))
108 if (character == EOF)
109 {
110 confirm_file_EOF = 1;
111 fputc ('\n', stdlis);
112 fflush (stdlis);
113 break;
114 }
115 return reply == 'y' || reply == 'Y';
116 }
117 }
118 \f
119 /* Options. */
120
121 /* For long options that unconditionally set a single flag, we have getopt
122 do it. For the others, we share the code for the equivalent short
123 named option, the name of which is stored in the otherwise-unused `val'
124 field of the `struct option'; for long options that have no equivalent
125 short option, we use non-characters as pseudo short options,
126 starting at CHAR_MAX + 1 and going upwards. */
127
128 enum
129 {
130 BACKUP_OPTION = CHAR_MAX + 1,
131 DELETE_OPTION,
132 EXCLUDE_OPTION,
133 GROUP_OPTION,
134 MODE_OPTION,
135 NEWER_MTIME_OPTION,
136 NULL_OPTION,
137 OVERWRITE_OPTION,
138 OWNER_OPTION,
139 POSIX_OPTION,
140 PRESERVE_OPTION,
141 RECORD_SIZE_OPTION,
142 RSH_COMMAND_OPTION,
143 SUFFIX_OPTION,
144 USE_COMPRESS_PROGRAM_OPTION,
145 VOLNO_FILE_OPTION,
146
147 /* Some cleanup is being made in GNU tar long options. Using old names is
148 allowed for a while, but will also send a warning to stderr. Take old
149 names out in 1.14, or in summer 1997, whichever happens last. */
150
151 OBSOLETE_ABSOLUTE_NAMES,
152 OBSOLETE_BLOCK_COMPRESS,
153 OBSOLETE_BLOCKING_FACTOR,
154 OBSOLETE_BLOCK_NUMBER,
155 OBSOLETE_READ_FULL_RECORDS,
156 OBSOLETE_TOUCH,
157 OBSOLETE_VERSION_CONTROL
158 };
159
160 /* If nonzero, display usage information and exit. */
161 static int show_help;
162
163 /* If nonzero, print the version on standard output and exit. */
164 static int show_version;
165
166 static struct option long_options[] =
167 {
168 {"absolute-names", no_argument, 0, 'P'},
169 {"absolute-paths", no_argument, 0, OBSOLETE_ABSOLUTE_NAMES},
170 {"after-date", required_argument, 0, 'N'},
171 {"append", no_argument, 0, 'r'},
172 {"atime-preserve", no_argument, &atime_preserve_option, 1},
173 {"backup", optional_argument, 0, BACKUP_OPTION},
174 {"block-compress", no_argument, 0, OBSOLETE_BLOCK_COMPRESS},
175 {"block-number", no_argument, 0, 'R'},
176 {"block-size", required_argument, 0, OBSOLETE_BLOCKING_FACTOR},
177 {"blocking-factor", required_argument, 0, 'b'},
178 {"bzip2", no_argument, 0, 'j'},
179 {"catenate", no_argument, 0, 'A'},
180 {"checkpoint", no_argument, &checkpoint_option, 1},
181 {"compare", no_argument, 0, 'd'},
182 {"compress", no_argument, 0, 'Z'},
183 {"concatenate", no_argument, 0, 'A'},
184 {"confirmation", no_argument, 0, 'w'},
185 /* FIXME: --selective as a synonym for --confirmation? */
186 {"create", no_argument, 0, 'c'},
187 {"delete", no_argument, 0, DELETE_OPTION},
188 {"dereference", no_argument, 0, 'h'},
189 {"diff", no_argument, 0, 'd'},
190 {"directory", required_argument, 0, 'C'},
191 {"exclude", required_argument, 0, EXCLUDE_OPTION},
192 {"exclude-from", required_argument, 0, 'X'},
193 {"extract", no_argument, 0, 'x'},
194 {"file", required_argument, 0, 'f'},
195 {"files-from", required_argument, 0, 'T'},
196 {"force-local", no_argument, &force_local_option, 1},
197 {"get", no_argument, 0, 'x'},
198 {"group", required_argument, 0, GROUP_OPTION},
199 {"gunzip", no_argument, 0, 'z'},
200 {"gzip", no_argument, 0, 'z'},
201 {"help", no_argument, &show_help, 1},
202 {"ignore-failed-read", no_argument, &ignore_failed_read_option, 1},
203 {"ignore-zeros", no_argument, 0, 'i'},
204 /* FIXME: --ignore-end as a new name for --ignore-zeros? */
205 {"incremental", no_argument, 0, 'G'},
206 {"info-script", required_argument, 0, 'F'},
207 {"interactive", no_argument, 0, 'w'},
208 {"keep-old-files", no_argument, 0, 'k'},
209 {"label", required_argument, 0, 'V'},
210 {"list", no_argument, 0, 't'},
211 {"listed-incremental", required_argument, 0, 'g'},
212 {"mode", required_argument, 0, MODE_OPTION},
213 {"modification-time", no_argument, 0, OBSOLETE_TOUCH},
214 {"multi-volume", no_argument, 0, 'M'},
215 {"new-volume-script", required_argument, 0, 'F'},
216 {"newer", required_argument, 0, 'N'},
217 {"newer-mtime", required_argument, 0, NEWER_MTIME_OPTION},
218 {"null", no_argument, 0, NULL_OPTION},
219 {"no-recursion", no_argument, &recursion_option, 0},
220 {"no-same-owner", no_argument, &same_owner_option, -1},
221 {"no-same-permissions", no_argument, &same_permissions_option, -1},
222 {"numeric-owner", no_argument, &numeric_owner_option, 1},
223 {"old-archive", no_argument, 0, 'o'},
224 {"one-file-system", no_argument, 0, 'l'},
225 {"overwrite", no_argument, 0, OVERWRITE_OPTION},
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 /* Print a usage message and exit with STATUS. */
266 static void
267 usage (int status)
268 {
269 if (status != TAREXIT_SUCCESS)
270 fprintf (stderr, _("Try `%s --help' for more information.\n"),
271 program_name);
272 else
273 {
274 fputs (_("\
275 GNU `tar' saves many files together into a single tape or disk archive, and\n\
276 can restore individual files from the archive.\n"),
277 stdout);
278 printf (_("\nUsage: %s [OPTION]... [FILE]...\n\
279 \n\
280 Examples:\n\
281 %s -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
282 %s -tvf archive.tar # List all files in archive.tar verbosely.\n\
283 %s -xf archive.tar # Extract all files from archive.tar.\n"),
284 program_name, program_name, program_name, program_name);
285 fputs (_("\
286 \n\
287 If a long option shows an argument as mandatory, then it is mandatory\n\
288 for the equivalent short option also. Similarly for optional arguments.\n"),
289 stdout);
290 fputs(_("\
291 \n\
292 Main operation mode:\n\
293 -t, --list list the contents of an archive\n\
294 -x, --extract, --get extract files from an archive\n\
295 -c, --create create a new archive\n\
296 -d, --diff, --compare find differences between archive and file system\n\
297 -r, --append append files to the end of an archive\n\
298 -u, --update only append files newer than copy in archive\n\
299 -A, --catenate append tar files to an archive\n\
300 --concatenate same as -A\n\
301 --delete delete from the archive (not on mag tapes!)\n"),
302 stdout);
303 fputs (_("\
304 \n\
305 Operation modifiers:\n\
306 -W, --verify attempt to verify the archive after writing it\n\
307 --remove-files remove files after adding them to the archive\n\
308 -k, --keep-old-files don't replace existing files when extracting\n\
309 --overwrite overwrite existing files when extracting\n\
310 -U, --unlink-first remove each file prior to extracting over it\n\
311 --recursive-unlink empty hierarchies prior to extracting directory\n\
312 -S, --sparse handle sparse files efficiently\n\
313 -O, --to-stdout extract files to standard output\n\
314 -G, --incremental handle old GNU-format incremental backup\n\
315 -g, --listed-incremental=FILE\n\
316 handle new GNU-format incremental backup\n\
317 --ignore-failed-read do not exit with nonzero on unreadable files\n"),
318 stdout);
319 fputs (_("\
320 \n\
321 Handling of file attributes:\n\
322 --owner=NAME force NAME as owner for added files\n\
323 --group=NAME force NAME as group for added files\n\
324 --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
325 --atime-preserve don't change access times on dumped files\n\
326 -m, --modification-time don't extract file modified time\n\
327 --same-owner try extracting files with the same ownership\n\
328 --no-same-owner extract files as yourself\n\
329 --numeric-owner always use numbers for user/group names\n\
330 -p, --same-permissions extract permissions information\n\
331 --no-same-permissions do not extract permissions information\n\
332 --preserve-permissions same as -p\n\
333 -s, --same-order sort names to extract to match archive\n\
334 --preserve-order same as -s\n\
335 --preserve same as both -p and -s\n"),
336 stdout);
337 fputs (_("\
338 \n\
339 Device selection and switching:\n\
340 -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
341 --force-local archive file is local even if has a colon\n\
342 --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
343 -[0-7][lmh] specify drive and density\n\
344 -M, --multi-volume create/list/extract multi-volume archive\n\
345 -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
346 -F, --info-script=FILE run script at end of each tape (implies -M)\n\
347 --new-volume-script=FILE same as -F FILE\n\
348 --volno-file=FILE use/update the volume number in FILE\n"),
349 stdout);
350 fputs (_("\
351 \n\
352 Device blocking:\n\
353 -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
354 --record-size=SIZE SIZE bytes per record, multiple of 512\n\
355 -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
356 -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
357 stdout);
358 fputs (_("\
359 \n\
360 Archive format selection:\n\
361 -V, --label=NAME create archive with volume name NAME\n\
362 PATTERN at list/extract time, a globbing PATTERN\n\
363 -o, --old-archive, --portability write a V7 format archive\n\
364 --posix write a POSIX format archive\n\
365 -j, --bzip2 filter the archive through bzip2\n\
366 -z, --gzip, --ungzip filter the archive through gzip\n\
367 -Z, --compress, --uncompress filter the archive through compress\n\
368 --use-compress-program=PROG filter through PROG (must accept -d)\n"),
369 stdout);
370 fputs (_("\
371 \n\
372 Local file selection:\n\
373 -C, --directory=DIR change to directory DIR\n\
374 -T, -I, --files-from=NAME get names to extract or create from file NAME\n\
375 --null -T reads null-terminated names, disable -C\n\
376 --exclude=PATTERN exclude files, given as a globbing PATTERN\n\
377 -X, --exclude-from=FILE exclude globbing patterns listed in FILE\n\
378 -P, --absolute-names don't strip leading `/'s from file names\n\
379 -h, --dereference dump instead the files symlinks point to\n\
380 --no-recursion avoid descending automatically in directories\n\
381 -l, --one-file-system stay in local file system when creating archive\n\
382 -K, --starting-file=NAME begin at file NAME in the archive\n"),
383 stdout);
384 #if !MSDOS
385 fputs (_("\
386 -N, --newer=DATE only store files newer than DATE\n\
387 --newer-mtime compare date and time when data changed only\n\
388 --after-date=DATE same as -N\n"),
389 stdout);
390 #endif
391 fputs (_("\
392 --backup[=CONTROL] backup before removal, choose version control\n\
393 --suffix=SUFFIX backup before removal, override usual suffix\n"),
394 stdout);
395 fputs (_("\
396 \n\
397 Informative output:\n\
398 --help print this help, then exit\n\
399 --version print tar program version number, then exit\n\
400 -v, --verbose verbosely list files processed\n\
401 --checkpoint print directory names while reading the archive\n\
402 --totals print total bytes written while creating archive\n\
403 -R, --block-number show block number within archive with each message\n\
404 -w, --interactive ask for confirmation for every action\n\
405 --confirmation same as -w\n"),
406 stdout);
407 fputs (_("\
408 \n\
409 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
410 The version control may be set with --backup or VERSION_CONTROL, values are:\n\
411 \n\
412 t, numbered make numbered backups\n\
413 nil, existing numbered if numbered backups exist, simple otherwise\n\
414 never, simple always make simple backups\n"),
415 stdout);
416 printf (_("\
417 \n\
418 GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
419 is set in the environment, GNU extensions are disallowed with `--posix'.\n\
420 Support for POSIX is only partially implemented, don't count on it yet.\n\
421 ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; and FILE may be a file\n\
422 or a device. *This* `tar' defaults to `-f%s -b%d'.\n"),
423 DEFAULT_ARCHIVE, DEFAULT_BLOCKING);
424 fputs (_("\nReport bugs to <bug-tar@gnu.org>.\n"), stdout);
425 }
426 exit (status);
427 }
428
429 /* Parse the options for tar. */
430
431 /* Available option letters are DEHJQY and aenqy. Some are reserved:
432
433 e exit immediately with a nonzero exit status if unexpected errors occur
434 E use extended headers (draft POSIX headers, that is)
435 n the archive is quickly seekable, so don't worry about random seeks
436 q stop after extracting the first occurrence of the named file
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:hijklmoprstuvwxz"
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 **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 recursion_option = FNM_LEADING_DIR;
493
494 owner_option = -1;
495 group_option = -1;
496
497 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
498
499 /* Convert old-style tar call by exploding option element and rearranging
500 options accordingly. */
501
502 if (argc > 1 && argv[1][0] != '-')
503 {
504 int new_argc; /* argc value for rearranged arguments */
505 char **new_argv; /* argv value for rearranged arguments */
506 char *const *in; /* cursor into original argv */
507 char **out; /* cursor into rearranged argv */
508 const char *letter; /* cursor into old option letters */
509 char buffer[3]; /* constructed option buffer */
510 const char *cursor; /* cursor in OPTION_STRING */
511
512 /* Initialize a constructed option. */
513
514 buffer[0] = '-';
515 buffer[2] = '\0';
516
517 /* Allocate a new argument array, and copy program name in it. */
518
519 new_argc = argc - 1 + strlen (argv[1]);
520 new_argv = xmalloc (new_argc * sizeof (char *));
521 in = argv;
522 out = new_argv;
523 *out++ = *in++;
524
525 /* Copy each old letter option as a separate option, and have the
526 corresponding argument moved next to it. */
527
528 for (letter = *in++; *letter; letter++)
529 {
530 buffer[1] = *letter;
531 *out++ = xstrdup (buffer);
532 cursor = strchr (OPTION_STRING, *letter);
533 if (cursor && cursor[1] == ':')
534 {
535 if (in < argv + argc)
536 *out++ = *in++;
537 else
538 USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
539 *letter));
540 }
541 }
542
543 /* Copy all remaining options. */
544
545 while (in < argv + argc)
546 *out++ = *in++;
547
548 /* Replace the old option list by the new one. */
549
550 argc = new_argc;
551 argv = new_argv;
552 }
553
554 /* Parse all options and non-options as they appear. */
555
556 input_files = 0;
557
558 prepend_default_options (getenv ("TAR_OPTIONS"), &argc, &argv);
559
560 while (optchar = getopt_long (argc, argv, OPTION_STRING, long_options, 0),
561 optchar != -1)
562 switch (optchar)
563 {
564 case '?':
565 usage (TAREXIT_FAILURE);
566
567 case 0:
568 break;
569
570 case 1:
571 /* File name or non-parsed option, because of RETURN_IN_ORDER
572 ordering triggered by the leading dash in OPTION_STRING. */
573
574 name_add (optarg);
575 input_files++;
576 break;
577
578 case 'A':
579 set_subcommand_option (CAT_SUBCOMMAND);
580 break;
581
582 case OBSOLETE_BLOCK_COMPRESS:
583 WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
584 break;
585
586 case OBSOLETE_BLOCKING_FACTOR:
587 WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
588 /* Fall through. */
589
590 case 'b':
591 {
592 uintmax_t u;
593 if (! (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
594 && u == (blocking_factor = u)
595 && 0 < blocking_factor
596 && u == (record_size = u * BLOCKSIZE) / BLOCKSIZE))
597 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
598 _("Invalid blocking factor")));
599 }
600 break;
601
602 case OBSOLETE_READ_FULL_RECORDS:
603 WARN ((0, 0,
604 _("Obsolete option name replaced by --read-full-records")));
605 /* Fall through. */
606
607 case 'B':
608 /* Try to reblock input records. For reading 4.2BSD pipes. */
609
610 /* It would surely make sense to exchange -B and -R, but it seems
611 that -B has been used for a long while in Sun tar ans most
612 BSD-derived systems. This is a consequence of the block/record
613 terminology confusion. */
614
615 read_full_records_option = 1;
616 break;
617
618 case 'c':
619 set_subcommand_option (CREATE_SUBCOMMAND);
620 break;
621
622 case 'C':
623 name_add ("-C");
624 name_add (optarg);
625 break;
626
627 case 'd':
628 set_subcommand_option (DIFF_SUBCOMMAND);
629 break;
630
631 case 'f':
632 if (archive_names == allocated_archive_names)
633 {
634 allocated_archive_names *= 2;
635 archive_name_array =
636 xrealloc (archive_name_array,
637 sizeof (const char *) * allocated_archive_names);
638 }
639 archive_name_array[archive_names++] = optarg;
640 break;
641
642 case 'F':
643 /* Since -F is only useful with -M, make it implied. Run this
644 script at the end of each tape. */
645
646 info_script_option = optarg;
647 multi_volume_option = 1;
648 break;
649
650 case 'g':
651 listed_incremental_option = optarg;
652 after_date_option = 1;
653 /* Fall through. */
654
655 case 'G':
656 /* We are making an incremental dump (FIXME: are we?); save
657 directories at the beginning of the archive, and include in each
658 directory its contents. */
659
660 incremental_option = 1;
661 break;
662
663 case 'h':
664 /* Follow symbolic links. */
665
666 dereference_option = 1;
667 break;
668
669 case 'i':
670 /* Ignore zero blocks (eofs). This can't be the default,
671 because Unix tar writes two blocks of zeros, then pads out
672 the record with garbage. */
673
674 ignore_zeros_option = 1;
675 break;
676
677 case 'j':
678 set_use_compress_program_option ("bzip2");
679 break;
680
681 case 'k':
682 /* Don't replace existing files. */
683 old_files_option = KEEP_OLD_FILES;
684 break;
685
686 case 'K':
687 starting_file_option = 1;
688 addname (optarg, 0);
689 break;
690
691 case 'l':
692 /* When dumping directories, don't dump files/subdirectories
693 that are on other filesystems. */
694
695 one_file_system_option = 1;
696 break;
697
698 case 'L':
699 {
700 uintmax_t u;
701 if (xstrtoumax (optarg, 0, 10, &u, "") != LONGINT_OK)
702 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
703 _("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 case 'I': /* for compatibility with Solaris tar */
800 files_from_option = optarg;
801 break;
802
803 case 'u':
804 set_subcommand_option (UPDATE_SUBCOMMAND);
805 break;
806
807 case 'U':
808 old_files_option = UNLINK_FIRST_OLD_FILES;
809 break;
810
811 case 'v':
812 verbose_option++;
813 break;
814
815 case 'V':
816 volume_label_option = optarg;
817 break;
818
819 case 'w':
820 interactive_option = 1;
821 break;
822
823 case 'W':
824 verify_option = 1;
825 break;
826
827 case 'x':
828 set_subcommand_option (EXTRACT_SUBCOMMAND);
829 break;
830
831 case 'X':
832 if (add_exclude_file (add_filtered_exclude, 0, optarg, '\n') != 0)
833 {
834 int e = errno;
835 FATAL_ERROR ((0, e, "%s", quotearg_colon (optarg)));
836 }
837 break;
838
839 case 'z':
840 set_use_compress_program_option ("gzip");
841 break;
842
843 case 'Z':
844 set_use_compress_program_option ("compress");
845 break;
846
847 case OBSOLETE_VERSION_CONTROL:
848 WARN ((0, 0, _("Obsolete option name replaced by --backup")));
849 /* Fall through. */
850
851 case BACKUP_OPTION:
852 backup_option = 1;
853 if (optarg)
854 version_control_string = optarg;
855 break;
856
857 case DELETE_OPTION:
858 set_subcommand_option (DELETE_SUBCOMMAND);
859 break;
860
861 case EXCLUDE_OPTION:
862 add_filtered_exclude (0, optarg);
863 break;
864
865 case GROUP_OPTION:
866 if (! (strlen (optarg) < GNAME_FIELD_SIZE
867 && gname_to_gid (optarg, &group_option)))
868 {
869 uintmax_t g;
870 if (xstrtoumax (optarg, 0, 10, &g, "") == LONGINT_OK
871 && g == (gid_t) g)
872 group_option = g;
873 else
874 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
875 _("%s: Invalid group")));
876 }
877 break;
878
879 case MODE_OPTION:
880 mode_option
881 = mode_compile (optarg,
882 MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
883 if (mode_option == MODE_INVALID)
884 FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
885 if (mode_option == MODE_MEMORY_EXHAUSTED)
886 xalloc_die ();
887 break;
888
889 case NULL_OPTION:
890 filename_terminator = '\0';
891 break;
892
893 case OVERWRITE_OPTION:
894 old_files_option = OVERWRITE_OLD_FILES;
895 break;
896
897 case OWNER_OPTION:
898 if (! (strlen (optarg) < UNAME_FIELD_SIZE
899 && uname_to_uid (optarg, &owner_option)))
900 {
901 uintmax_t u;
902 if (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
903 && u == (uid_t) u)
904 owner_option = u;
905 else
906 FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
907 _("Invalid owner")));
908 }
909 break;
910
911 case POSIX_OPTION:
912 #if OLDGNU_COMPATIBILITY
913 if (archive_format == DEFAULT_FORMAT)
914 archive_format = GNU_FORMAT;
915 else if (archive_format != GNU_FORMAT)
916 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
917 #else
918 if (archive_format == DEFAULT_FORMAT)
919 archive_format = POSIX_FORMAT;
920 else if (archive_format != POSIX_FORMAT)
921 USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
922 #endif
923 break;
924
925 case PRESERVE_OPTION:
926 same_permissions_option = 1;
927 same_order_option = 1;
928 break;
929
930 case RECORD_SIZE_OPTION:
931 {
932 uintmax_t u;
933 if (! (xstrtoumax (optarg, 0, 10, &u, "") == LONGINT_OK
934 && u == (size_t) u))
935 USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (optarg),
936 _("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 2000 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 (volume_label_option)
1108 {
1109 size_t volume_label_max_len =
1110 (sizeof current_header->header.name
1111 - 1 /* for trailing '\0' */
1112 - (multi_volume_option
1113 ? (sizeof " Volume "
1114 - 1 /* for null at end of " Volume " */
1115 + INT_STRLEN_BOUND (int) /* for volume number */
1116 - 1 /* for sign, as 0 <= volno */)
1117 : 0));
1118 if (volume_label_max_len < strlen (volume_label_option))
1119 USAGE_ERROR ((0, 0,
1120 _("%s: Volume label is too long (limit is %lu bytes)"),
1121 quotearg_colon (volume_label_option),
1122 (unsigned long) volume_label_max_len));
1123 }
1124
1125 /* If ready to unlink hierarchies, so we are for simpler files. */
1126 if (recursive_unlink_option)
1127 old_files_option = UNLINK_FIRST_OLD_FILES;
1128
1129 /* Forbid using -c with no input files whatsoever. Check that `-f -',
1130 explicit or implied, is used correctly. */
1131
1132 switch (subcommand_option)
1133 {
1134 case CREATE_SUBCOMMAND:
1135 if (input_files == 0 && !files_from_option)
1136 USAGE_ERROR ((0, 0,
1137 _("Cowardly refusing to create an empty archive")));
1138 break;
1139
1140 case EXTRACT_SUBCOMMAND:
1141 case LIST_SUBCOMMAND:
1142 case DIFF_SUBCOMMAND:
1143 for (archive_name_cursor = archive_name_array;
1144 archive_name_cursor < archive_name_array + archive_names;
1145 archive_name_cursor++)
1146 if (!strcmp (*archive_name_cursor, "-"))
1147 request_stdin ("-f");
1148 break;
1149
1150 case CAT_SUBCOMMAND:
1151 case UPDATE_SUBCOMMAND:
1152 case APPEND_SUBCOMMAND:
1153 for (archive_name_cursor = archive_name_array;
1154 archive_name_cursor < archive_name_array + archive_names;
1155 archive_name_cursor++)
1156 if (!strcmp (*archive_name_cursor, "-"))
1157 USAGE_ERROR ((0, 0,
1158 _("Options `-Aru' are incompatible with `-f -'")));
1159
1160 default:
1161 break;
1162 }
1163
1164 archive_name_cursor = archive_name_array;
1165
1166 /* Prepare for generating backup names. */
1167
1168 if (backup_suffix_string)
1169 simple_backup_suffix = xstrdup (backup_suffix_string);
1170
1171 if (backup_option)
1172 backup_type = xget_version ("--backup", version_control_string);
1173 }
1174 \f
1175 /* Tar proper. */
1176
1177 /* Main routine for tar. */
1178 int
1179 main (int argc, char **argv)
1180 {
1181 #if HAVE_CLOCK_GETTIME
1182 if (clock_gettime (CLOCK_REALTIME, &start_timespec) != 0)
1183 #endif
1184 start_time = time (0);
1185 program_name = argv[0];
1186 setlocale (LC_ALL, "");
1187 bindtextdomain (PACKAGE, LOCALEDIR);
1188 textdomain (PACKAGE);
1189
1190 exit_status = TAREXIT_SUCCESS;
1191 filename_terminator = '\n';
1192 set_quoting_style (0, escape_quoting_style);
1193
1194 /* Pre-allocate a few structures. */
1195
1196 allocated_archive_names = 10;
1197 archive_name_array =
1198 xmalloc (sizeof (const char *) * allocated_archive_names);
1199 archive_names = 0;
1200
1201 #ifdef SIGCHLD
1202 /* System V fork+wait does not work if SIGCHLD is ignored. */
1203 signal (SIGCHLD, SIG_DFL);
1204 #endif
1205
1206 init_names ();
1207
1208 /* Decode options. */
1209
1210 decode_options (argc, argv);
1211 name_init (argc, argv);
1212
1213 /* Main command execution. */
1214
1215 if (volno_file_option)
1216 init_volume_number ();
1217
1218 switch (subcommand_option)
1219 {
1220 case UNKNOWN_SUBCOMMAND:
1221 USAGE_ERROR ((0, 0,
1222 _("You must specify one of the `-Acdtrux' options")));
1223
1224 case CAT_SUBCOMMAND:
1225 case UPDATE_SUBCOMMAND:
1226 case APPEND_SUBCOMMAND:
1227 update_archive ();
1228 break;
1229
1230 case DELETE_SUBCOMMAND:
1231 delete_archive_members ();
1232 break;
1233
1234 case CREATE_SUBCOMMAND:
1235 create_archive ();
1236 name_close ();
1237
1238 if (totals_option)
1239 print_total_written ();
1240 break;
1241
1242 case EXTRACT_SUBCOMMAND:
1243 extr_init ();
1244 read_and (extract_archive);
1245 extract_finish ();
1246 break;
1247
1248 case LIST_SUBCOMMAND:
1249 read_and (list_archive);
1250 break;
1251
1252 case DIFF_SUBCOMMAND:
1253 diff_init ();
1254 read_and (diff_archive);
1255 break;
1256 }
1257
1258 if (volno_file_option)
1259 closeout_volume_number ();
1260
1261 /* Dispose of allocated memory, and return. */
1262
1263 free (archive_name_array);
1264 name_term ();
1265
1266 if (stdlis == stdout && (ferror (stdout) || fclose (stdout) != 0))
1267 FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
1268 if (exit_status == TAREXIT_FAILURE)
1269 error (0, 0, _("Error exit delayed from previous errors"));
1270 exit (exit_status);
1271 }
This page took 0.088273 seconds and 5 git commands to generate.