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