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