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