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