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