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