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