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