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