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