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