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