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