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