Import libarchive-3.0.4.
[dragonfly.git] / contrib / libarchive / tar / bsdtar.c
CommitLineData
63551f52 1/*-
8029ab02 2 * Copyright (c) 2003-2008 Tim Kientzle
63551f52
PA
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "bsdtar_platform.h"
8029ab02 27__FBSDID("$FreeBSD: src/usr.bin/tar/bsdtar.c,v 1.93 2008/11/08 04:43:24 kientzle Exp $");
63551f52
PA
28
29#ifdef HAVE_SYS_PARAM_H
30#include <sys/param.h>
31#endif
32#ifdef HAVE_SYS_STAT_H
33#include <sys/stat.h>
34#endif
c09f92d2
PA
35#ifdef HAVE_COPYFILE_H
36#include <copyfile.h>
37#endif
63551f52
PA
38#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
41#ifdef HAVE_FCNTL_H
42#include <fcntl.h>
43#endif
63551f52
PA
44#ifdef HAVE_LANGINFO_H
45#include <langinfo.h>
46#endif
47#ifdef HAVE_LOCALE_H
48#include <locale.h>
49#endif
50#ifdef HAVE_PATHS_H
51#include <paths.h>
52#endif
9c82a63e
PA
53#ifdef HAVE_SIGNAL_H
54#include <signal.h>
55#endif
63551f52
PA
56#include <stdio.h>
57#ifdef HAVE_STDLIB_H
58#include <stdlib.h>
59#endif
60#ifdef HAVE_STRING_H
61#include <string.h>
62#endif
63#ifdef HAVE_TIME_H
64#include <time.h>
65#endif
66#ifdef HAVE_UNISTD_H
67#include <unistd.h>
68#endif
63551f52
PA
69
70#include "bsdtar.h"
9c82a63e 71#include "err.h"
63551f52
PA
72
73/*
74 * Per POSIX.1-1988, tar defaults to reading/writing archives to/from
75 * the default tape device for the system. Pick something reasonable here.
76 */
77#ifdef __linux
78#define _PATH_DEFTAPE "/dev/st0"
79#endif
8029ab02
PA
80#if defined(_WIN32) && !defined(__CYGWIN__)
81#define _PATH_DEFTAPE "\\\\.\\tape0"
82#endif
c09f92d2
PA
83#if defined(__APPLE__)
84#undef _PATH_DEFTAPE
85#define _PATH_DEFTAPE "-" /* Mac OS has no tape support, default to stdio. */
86#endif
63551f52
PA
87
88#ifndef _PATH_DEFTAPE
89#define _PATH_DEFTAPE "/dev/tape"
90#endif
91
9c82a63e
PA
92#ifdef __MINGW32__
93int _CRT_glob = 0; /* Disable broken CRT globbing. */
94#endif
95
9c82a63e
PA
96#if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
97static volatile int siginfo_occurred;
98
99static void
100siginfo_handler(int sig)
101{
102 (void)sig; /* UNUSED */
103 siginfo_occurred = 1;
104}
105
106int
107need_report(void)
108{
109 int r = siginfo_occurred;
110 siginfo_occurred = 0;
111 return (r);
112}
113#else
114int
115need_report(void)
116{
117 return (0);
118}
119#endif
120
9c82a63e 121static void long_help(void);
63551f52
PA
122static void only_mode(struct bsdtar *, const char *opt,
123 const char *valid);
63551f52
PA
124static void set_mode(struct bsdtar *, char opt);
125static void version(void);
126
63551f52
PA
127/* A basic set of security flags to request from libarchive. */
128#define SECURITY \
129 (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
130 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
131
132int
133main(int argc, char **argv)
134{
135 struct bsdtar *bsdtar, bsdtar_storage;
63551f52
PA
136 int opt, t;
137 char option_o;
138 char possible_help_request;
139 char buff[16];
140
141 /*
142 * Use a pointer for consistency, but stack-allocated storage
143 * for ease of cleanup.
144 */
c09f92d2 145 bsdtar = &bsdtar_storage;
63551f52
PA
146 memset(bsdtar, 0, sizeof(*bsdtar));
147 bsdtar->fd = -1; /* Mark as "unused" */
c09f92d2
PA
148 bsdtar->gid = -1;
149 bsdtar->uid = -1;
63551f52 150 option_o = 0;
9c82a63e 151
c09f92d2
PA
152#if defined(HAVE_SIGACTION)
153 { /* Set up signal handling. */
9c82a63e
PA
154 struct sigaction sa;
155 sa.sa_handler = siginfo_handler;
156 sigemptyset(&sa.sa_mask);
157 sa.sa_flags = 0;
158#ifdef SIGINFO
159 if (sigaction(SIGINFO, &sa, NULL))
160 lafe_errc(1, errno, "sigaction(SIGINFO) failed");
161#endif
162#ifdef SIGUSR1
163 /* ... and treat SIGUSR1 the same way as SIGINFO. */
164 if (sigaction(SIGUSR1, &sa, NULL))
165 lafe_errc(1, errno, "sigaction(SIGUSR1) failed");
c09f92d2
PA
166#endif
167#ifdef SIGPIPE
168 /* Ignore SIGPIPE signals. */
169 sa.sa_handler = SIG_IGN;
170 sigaction(SIGPIPE, &sa, NULL);
9c82a63e
PA
171#endif
172 }
8029ab02 173#endif
63551f52 174
9c82a63e
PA
175
176 /* Need lafe_progname before calling lafe_warnc. */
63551f52 177 if (*argv == NULL)
9c82a63e 178 lafe_progname = "bsdtar";
63551f52 179 else {
8029ab02 180#if defined(_WIN32) && !defined(__CYGWIN__)
9c82a63e 181 lafe_progname = strrchr(*argv, '\\');
59bf7050 182 if (strrchr(*argv, '/') > lafe_progname)
8029ab02 183#endif
59bf7050 184 lafe_progname = strrchr(*argv, '/');
9c82a63e
PA
185 if (lafe_progname != NULL)
186 lafe_progname++;
63551f52 187 else
9c82a63e 188 lafe_progname = *argv;
63551f52
PA
189 }
190
9c82a63e 191#if HAVE_SETLOCALE
63551f52 192 if (setlocale(LC_ALL, "") == NULL)
9c82a63e
PA
193 lafe_warnc(0, "Failed to set default locale");
194#endif
63551f52
PA
195#if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
196 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
197#endif
198 possible_help_request = 0;
199
200 /* Look up uid of current user for future reference */
201 bsdtar->user_uid = geteuid();
202
203 /* Default: open tape drive. */
204 bsdtar->filename = getenv("TAPE");
205 if (bsdtar->filename == NULL)
206 bsdtar->filename = _PATH_DEFTAPE;
207
c09f92d2
PA
208 /* Default block size settings. */
209 bsdtar->bytes_per_block = DEFAULT_BYTES_PER_BLOCK;
210 /* Allow library to default this unless user specifies -b. */
211 bsdtar->bytes_in_last_block = -1;
212
63551f52
PA
213 /* Default: preserve mod time on extract */
214 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
215
216 /* Default: Perform basic security checks. */
217 bsdtar->extract_flags |= SECURITY;
218
9c82a63e
PA
219#ifndef _WIN32
220 /* On POSIX systems, assume --same-owner and -p when run by
221 * the root user. This doesn't make any sense on Windows. */
222 if (bsdtar->user_uid == 0) {
63551f52
PA
223 /* --same-owner */
224 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
225 /* -p */
226 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
227 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
228 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
229 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
c09f92d2 230 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
63551f52 231 }
9c82a63e 232#endif
63551f52 233
c09f92d2
PA
234 /*
235 * Enable Mac OS "copyfile()" extension by default.
236 * This has no effect on other platforms.
237 */
59bf7050 238 bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE;
c09f92d2
PA
239#ifdef COPYFILE_DISABLE_VAR
240 if (getenv(COPYFILE_DISABLE_VAR))
59bf7050 241 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
c09f92d2 242#endif
59bf7050
PA
243 bsdtar->matching = archive_match_new();
244 if (bsdtar->matching == NULL)
245 lafe_errc(1, errno, "Out of memory");
c09f92d2 246
63551f52
PA
247 bsdtar->argv = argv;
248 bsdtar->argc = argc;
249
63551f52
PA
250 /*
251 * Comments following each option indicate where that option
252 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
253 * no such comment, then I don't know of anyone else who
254 * implements that option.
255 */
8029ab02 256 while ((opt = bsdtar_getopt(bsdtar)) != -1) {
63551f52
PA
257 switch (opt) {
258 case 'B': /* GNU tar */
259 /* libarchive doesn't need this; just ignore it. */
260 break;
261 case 'b': /* SUSv2 */
c09f92d2 262 t = atoi(bsdtar->argument);
9c82a63e
PA
263 if (t <= 0 || t > 8192)
264 lafe_errc(1, 0,
265 "Argument to -b is out of range (1..8192)");
63551f52 266 bsdtar->bytes_per_block = 512 * t;
c09f92d2
PA
267 /* Explicit -b forces last block size. */
268 bsdtar->bytes_in_last_block = bsdtar->bytes_per_block;
63551f52
PA
269 break;
270 case 'C': /* GNU tar */
c09f92d2
PA
271 if (strlen(bsdtar->argument) == 0)
272 lafe_errc(1, 0,
273 "Meaningless option: -C ''");
274
275 set_chdir(bsdtar, bsdtar->argument);
63551f52
PA
276 break;
277 case 'c': /* SUSv2 */
278 set_mode(bsdtar, opt);
279 break;
280 case OPTION_CHECK_LINKS: /* GNU tar */
281 bsdtar->option_warn_links = 1;
282 break;
8bf2d2ac
PA
283 case OPTION_CHROOT: /* NetBSD */
284 bsdtar->option_chroot = 1;
285 break;
c09f92d2 286 case OPTION_DISABLE_COPYFILE: /* Mac OS X */
59bf7050 287 bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
c09f92d2 288 break;
63551f52 289 case OPTION_EXCLUDE: /* GNU tar */
59bf7050
PA
290 if (archive_match_exclude_pattern(
291 bsdtar->matching, bsdtar->argument) != ARCHIVE_OK)
9c82a63e 292 lafe_errc(1, 0,
c09f92d2 293 "Couldn't exclude %s\n", bsdtar->argument);
63551f52
PA
294 break;
295 case OPTION_FORMAT: /* GNU tar, others */
c09f92d2 296 bsdtar->create_format = bsdtar->argument;
63551f52
PA
297 break;
298 case 'f': /* SUSv2 */
c09f92d2
PA
299 bsdtar->filename = bsdtar->argument;
300 break;
301 case OPTION_GID: /* cpio */
302 t = atoi(bsdtar->argument);
303 if (t < 0)
304 lafe_errc(1, 0,
305 "Argument to --gid must be positive");
306 bsdtar->gid = t;
307 break;
308 case OPTION_GNAME: /* cpio */
309 bsdtar->gname = bsdtar->argument;
63551f52 310 break;
63551f52
PA
311 case 'H': /* BSD convention */
312 bsdtar->symlink_mode = 'H';
313 break;
314 case 'h': /* Linux Standards Base, gtar; synonym for -L */
315 bsdtar->symlink_mode = 'L';
316 /* Hack: -h by itself is the "help" command. */
317 possible_help_request = 1;
318 break;
319 case OPTION_HELP: /* GNU tar, others */
9c82a63e 320 long_help();
63551f52
PA
321 exit(0);
322 break;
323 case 'I': /* GNU tar */
324 /*
325 * TODO: Allow 'names' to come from an archive,
326 * not just a text file. Design a good UI for
327 * allowing names and mode/owner to be read
328 * from an archive, with contents coming from
329 * disk. This can be used to "refresh" an
330 * archive or to design archives with special
331 * permissions without having to create those
332 * permissions on disk.
333 */
c09f92d2 334 bsdtar->names_from_file = bsdtar->argument;
63551f52
PA
335 break;
336 case OPTION_INCLUDE:
337 /*
55c601bb
PA
338 * No one else has the @archive extension, so
339 * no one else needs this to filter entries
63551f52
PA
340 * when transforming archives.
341 */
59bf7050
PA
342 if (archive_match_include_pattern(bsdtar->matching,
343 bsdtar->argument) != ARCHIVE_OK)
9c82a63e 344 lafe_errc(1, 0,
63551f52 345 "Failed to add %s to inclusion list",
c09f92d2 346 bsdtar->argument);
63551f52
PA
347 break;
348 case 'j': /* GNU tar */
63551f52 349 if (bsdtar->create_compression != '\0')
9c82a63e 350 lafe_errc(1, 0,
63551f52
PA
351 "Can't specify both -%c and -%c", opt,
352 bsdtar->create_compression);
353 bsdtar->create_compression = opt;
8029ab02
PA
354 break;
355 case 'J': /* GNU tar 1.21 and later */
8029ab02 356 if (bsdtar->create_compression != '\0')
9c82a63e 357 lafe_errc(1, 0,
8029ab02
PA
358 "Can't specify both -%c and -%c", opt,
359 bsdtar->create_compression);
360 bsdtar->create_compression = opt;
63551f52
PA
361 break;
362 case 'k': /* GNU tar */
363 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
364 break;
8bf2d2ac
PA
365 case OPTION_KEEP_NEWER_FILES: /* GNU tar */
366 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
367 break;
63551f52
PA
368 case 'L': /* BSD convention */
369 bsdtar->symlink_mode = 'L';
370 break;
6ed811d1
PA
371 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
372 /* GNU tar 1.13 used -l for --one-file-system */
373 bsdtar->option_warn_links = 1;
63551f52 374 break;
c09f92d2
PA
375 case OPTION_LZIP: /* GNU tar beginning with 1.23 */
376 case OPTION_LZMA: /* GNU tar beginning with 1.20 */
8029ab02 377 if (bsdtar->create_compression != '\0')
9c82a63e 378 lafe_errc(1, 0,
8029ab02
PA
379 "Can't specify both -%c and -%c", opt,
380 bsdtar->create_compression);
381 bsdtar->create_compression = opt;
8029ab02 382 break;
63551f52
PA
383 case 'm': /* SUSv2 */
384 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
385 break;
386 case 'n': /* GNU tar */
387 bsdtar->option_no_subdirs = 1;
388 break;
389 /*
390 * Selecting files by time:
391 * --newer-?time='date' Only files newer than 'date'
392 * --newer-?time-than='file' Only files newer than time
393 * on specified file (useful for incremental backups)
394 * TODO: Add corresponding "older" options to reverse these.
395 */
396 case OPTION_NEWER_CTIME: /* GNU tar */
59bf7050
PA
397 if (archive_match_include_date(bsdtar->matching,
398 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
399 bsdtar->argument) != ARCHIVE_OK)
400 lafe_errc(1, 0, "Error : %s",
401 archive_error_string(bsdtar->matching));
63551f52
PA
402 break;
403 case OPTION_NEWER_CTIME_THAN:
59bf7050
PA
404 if (archive_match_include_file_time(bsdtar->matching,
405 ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
406 bsdtar->argument) != ARCHIVE_OK)
407 lafe_errc(1, 0, "Error : %s",
408 archive_error_string(bsdtar->matching));
63551f52
PA
409 break;
410 case OPTION_NEWER_MTIME: /* GNU tar */
59bf7050
PA
411 if (archive_match_include_date(bsdtar->matching,
412 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
413 bsdtar->argument) != ARCHIVE_OK)
414 lafe_errc(1, 0, "Error : %s",
415 archive_error_string(bsdtar->matching));
63551f52
PA
416 break;
417 case OPTION_NEWER_MTIME_THAN:
59bf7050
PA
418 if (archive_match_include_file_time(bsdtar->matching,
419 ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
420 bsdtar->argument) != ARCHIVE_OK)
421 lafe_errc(1, 0, "Error : %s",
422 archive_error_string(bsdtar->matching));
63551f52
PA
423 break;
424 case OPTION_NODUMP: /* star */
59bf7050 425 bsdtar->readdisk_flags |= ARCHIVE_READDISK_HONOR_NODUMP;
63551f52
PA
426 break;
427 case OPTION_NO_SAME_OWNER: /* GNU tar */
428 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
429 break;
430 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
431 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
432 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
433 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
434 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
c09f92d2 435 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA;
63551f52
PA
436 break;
437 case OPTION_NULL: /* GNU tar */
438 bsdtar->option_null++;
439 break;
8bf2d2ac 440 case OPTION_NUMERIC_OWNER: /* GNU tar */
c09f92d2
PA
441 bsdtar->uname = "";
442 bsdtar->gname = "";
8bf2d2ac
PA
443 bsdtar->option_numeric_owner++;
444 break;
63551f52
PA
445 case 'O': /* GNU tar */
446 bsdtar->option_stdout = 1;
447 break;
448 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
449 option_o = 1; /* Record it and resolve it later. */
450 break;
451 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
59bf7050
PA
452 bsdtar->readdisk_flags |=
453 ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS;
63551f52 454 break;
c09f92d2
PA
455 case OPTION_OPTIONS:
456 bsdtar->option_options = bsdtar->argument;
457 break;
63551f52
PA
458#if 0
459 /*
460 * The common BSD -P option is not necessary, since
461 * our default is to archive symlinks, not follow
462 * them. This is convenient, as -P conflicts with GNU
463 * tar anyway.
464 */
465 case 'P': /* BSD convention */
466 /* Default behavior, no option necessary. */
467 break;
468#endif
469 case 'P': /* GNU tar */
470 bsdtar->extract_flags &= ~SECURITY;
471 bsdtar->option_absolute_paths = 1;
472 break;
473 case 'p': /* GNU tar, star */
474 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
475 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
476 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
477 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
c09f92d2 478 bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
63551f52 479 break;
a44e961d
PA
480 case OPTION_POSIX: /* GNU tar */
481 bsdtar->create_format = "pax";
482 break;
8bf2d2ac
PA
483 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
484 bsdtar->option_fast_read = 1;
485 break;
63551f52
PA
486 case 'r': /* SUSv2 */
487 set_mode(bsdtar, opt);
488 break;
8bf2d2ac
PA
489 case 'S': /* NetBSD pax-as-tar */
490 bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
491 break;
492 case 's': /* NetBSD pax-as-tar */
493#if HAVE_REGEX_H
c09f92d2 494 add_substitution(bsdtar, bsdtar->argument);
8bf2d2ac 495#else
9c82a63e 496 lafe_warnc(0,
8029ab02 497 "-s is not supported by this version of bsdtar");
9c82a63e 498 usage();
8bf2d2ac
PA
499#endif
500 break;
8029ab02
PA
501 case OPTION_SAME_OWNER: /* GNU tar */
502 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
503 break;
63551f52 504 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
c09f92d2
PA
505 errno = 0;
506 bsdtar->strip_components = strtol(bsdtar->argument,
507 NULL, 0);
508 if (errno)
509 lafe_errc(1, 0,
510 "Invalid --strip-components argument: %s",
511 bsdtar->argument);
63551f52
PA
512 break;
513 case 'T': /* GNU tar */
c09f92d2 514 bsdtar->names_from_file = bsdtar->argument;
63551f52
PA
515 break;
516 case 't': /* SUSv2 */
517 set_mode(bsdtar, opt);
518 bsdtar->verbose++;
519 break;
520 case OPTION_TOTALS: /* GNU tar */
521 bsdtar->option_totals++;
522 break;
523 case 'U': /* GNU tar */
524 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
525 bsdtar->option_unlink_first = 1;
526 break;
527 case 'u': /* SUSv2 */
528 set_mode(bsdtar, opt);
529 break;
c09f92d2
PA
530 case OPTION_UID: /* cpio */
531 t = atoi(bsdtar->argument);
532 if (t < 0)
533 lafe_errc(1, 0,
534 "Argument to --uid must be positive");
535 bsdtar->uid = t;
536 break;
537 case OPTION_UNAME: /* cpio */
538 bsdtar->uname = bsdtar->argument;
539 break;
63551f52
PA
540 case 'v': /* SUSv2 */
541 bsdtar->verbose++;
542 break;
543 case OPTION_VERSION: /* GNU convention */
544 version();
545 break;
546#if 0
547 /*
548 * The -W longopt feature is handled inside of
8029ab02 549 * bsdtar_getopt(), so -W is not available here.
63551f52 550 */
8029ab02 551 case 'W': /* Obscure GNU convention. */
63551f52
PA
552 break;
553#endif
554 case 'w': /* SUSv2 */
555 bsdtar->option_interactive = 1;
556 break;
557 case 'X': /* GNU tar */
59bf7050
PA
558 if (archive_match_exclude_pattern_from_file(
559 bsdtar->matching, bsdtar->argument, 0)
560 != ARCHIVE_OK)
561 lafe_errc(1, 0, "Error : %s",
562 archive_error_string(bsdtar->matching));
63551f52
PA
563 break;
564 case 'x': /* SUSv2 */
565 set_mode(bsdtar, opt);
566 break;
567 case 'y': /* FreeBSD version of GNU tar */
63551f52 568 if (bsdtar->create_compression != '\0')
9c82a63e 569 lafe_errc(1, 0,
63551f52
PA
570 "Can't specify both -%c and -%c", opt,
571 bsdtar->create_compression);
572 bsdtar->create_compression = opt;
63551f52
PA
573 break;
574 case 'Z': /* GNU tar */
575 if (bsdtar->create_compression != '\0')
9c82a63e 576 lafe_errc(1, 0,
63551f52
PA
577 "Can't specify both -%c and -%c", opt,
578 bsdtar->create_compression);
579 bsdtar->create_compression = opt;
580 break;
581 case 'z': /* GNU tar, star, many others */
63551f52 582 if (bsdtar->create_compression != '\0')
9c82a63e 583 lafe_errc(1, 0,
63551f52
PA
584 "Can't specify both -%c and -%c", opt,
585 bsdtar->create_compression);
586 bsdtar->create_compression = opt;
63551f52
PA
587 break;
588 case OPTION_USE_COMPRESS_PROGRAM:
c09f92d2 589 bsdtar->compress_program = bsdtar->argument;
63551f52
PA
590 break;
591 default:
9c82a63e 592 usage();
63551f52
PA
593 }
594 }
595
596 /*
597 * Sanity-check options.
598 */
599
600 /* If no "real" mode was specified, treat -h as --help. */
601 if ((bsdtar->mode == '\0') && possible_help_request) {
9c82a63e 602 long_help();
63551f52
PA
603 exit(0);
604 }
605
606 /* Otherwise, a mode is required. */
607 if (bsdtar->mode == '\0')
9c82a63e 608 lafe_errc(1, 0,
63551f52
PA
609 "Must specify one of -c, -r, -t, -u, -x");
610
611 /* Check boolean options only permitted in certain modes. */
59bf7050 612 if (bsdtar->readdisk_flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS)
63551f52
PA
613 only_mode(bsdtar, "--one-file-system", "cru");
614 if (bsdtar->option_fast_read)
615 only_mode(bsdtar, "--fast-read", "xt");
59bf7050 616 if (bsdtar->readdisk_flags & ARCHIVE_READDISK_HONOR_NODUMP)
63551f52
PA
617 only_mode(bsdtar, "--nodump", "cru");
618 if (option_o > 0) {
619 switch (bsdtar->mode) {
620 case 'c':
621 /*
622 * In GNU tar, -o means "old format." The
623 * "ustar" format is the closest thing
624 * supported by libarchive.
625 */
626 bsdtar->create_format = "ustar";
627 /* TODO: bsdtar->create_format = "v7"; */
628 break;
629 case 'x':
630 /* POSIX-compatible behavior. */
631 bsdtar->option_no_owner = 1;
632 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
633 break;
634 default:
635 only_mode(bsdtar, "-o", "xc");
636 break;
637 }
638 }
639 if (bsdtar->option_no_subdirs)
640 only_mode(bsdtar, "-n", "cru");
641 if (bsdtar->option_stdout)
642 only_mode(bsdtar, "-O", "xt");
643 if (bsdtar->option_unlink_first)
644 only_mode(bsdtar, "-U", "x");
645 if (bsdtar->option_warn_links)
646 only_mode(bsdtar, "--check-links", "cr");
647
648 /* Check other parameters only permitted in certain modes. */
63551f52
PA
649 if (bsdtar->create_compression != '\0') {
650 strcpy(buff, "-?");
651 buff[1] = bsdtar->create_compression;
652 only_mode(bsdtar, buff, "cxt");
653 }
654 if (bsdtar->create_format != NULL)
8bf2d2ac 655 only_mode(bsdtar, "--format", "cru");
63551f52
PA
656 if (bsdtar->symlink_mode != '\0') {
657 strcpy(buff, "-?");
658 buff[1] = bsdtar->symlink_mode;
659 only_mode(bsdtar, buff, "cru");
660 }
c09f92d2
PA
661
662 /* Filename "-" implies stdio. */
663 if (strcmp(bsdtar->filename, "-") == 0)
664 bsdtar->filename = NULL;
63551f52 665
63551f52
PA
666 switch(bsdtar->mode) {
667 case 'c':
668 tar_mode_c(bsdtar);
669 break;
670 case 'r':
671 tar_mode_r(bsdtar);
672 break;
673 case 't':
674 tar_mode_t(bsdtar);
675 break;
676 case 'u':
677 tar_mode_u(bsdtar);
678 break;
679 case 'x':
680 tar_mode_x(bsdtar);
681 break;
682 }
683
59bf7050 684 archive_match_free(bsdtar->matching);
8bf2d2ac
PA
685#if HAVE_REGEX_H
686 cleanup_substitution(bsdtar);
687#endif
688
63551f52 689 if (bsdtar->return_value != 0)
9c82a63e 690 lafe_warnc(0,
63551f52
PA
691 "Error exit delayed from previous errors.");
692 return (bsdtar->return_value);
693}
694
695static void
696set_mode(struct bsdtar *bsdtar, char opt)
697{
698 if (bsdtar->mode != '\0' && bsdtar->mode != opt)
9c82a63e 699 lafe_errc(1, 0,
63551f52
PA
700 "Can't specify both -%c and -%c", opt, bsdtar->mode);
701 bsdtar->mode = opt;
702}
703
704/*
705 * Verify that the mode is correct.
706 */
707static void
708only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
709{
710 if (strchr(valid_modes, bsdtar->mode) == NULL)
9c82a63e 711 lafe_errc(1, 0,
63551f52
PA
712 "Option %s is not permitted in mode -%c",
713 opt, bsdtar->mode);
714}
715
716
63551f52 717void
9c82a63e 718usage(void)
63551f52
PA
719{
720 const char *p;
721
9c82a63e 722 p = lafe_progname;
63551f52
PA
723
724 fprintf(stderr, "Usage:\n");
725 fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
726 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
727 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
63551f52 728 fprintf(stderr, " Help: %s --help\n", p);
63551f52
PA
729 exit(1);
730}
731
732static void
733version(void)
734{
a44e961d
PA
735 printf("bsdtar %s - %s\n",
736 BSDTAR_VERSION_STRING,
c09f92d2 737 archive_version_string());
8bf2d2ac 738 exit(0);
63551f52
PA
739}
740
741static const char *long_help_msg =
742 "First option must be a mode specifier:\n"
743 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
744 "Common Options:\n"
745 " -b # Use # 512-byte records per I/O block\n"
746 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
747 " -v Verbose\n"
748 " -w Interactive\n"
749 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
750 " <file>, <dir> add these items to archive\n"
8029ab02 751 " -z, -j, -J, --lzma Compress archive with gzip/bzip2/xz/lzma\n"
63551f52 752 " --format {ustar|pax|cpio|shar} Select archive format\n"
63551f52 753 " --exclude <pattern> Skip files that match pattern\n"
63551f52
PA
754 " -C <dir> Change to <dir> before processing remaining files\n"
755 " @<archive> Add entries from <archive> to output\n"
756 "List: %p -t [options] [<patterns>]\n"
757 " <patterns> If specified, list only entries that match\n"
758 "Extract: %p -x [options] [<patterns>]\n"
759 " <patterns> If specified, extract only entries that match\n"
760 " -k Keep (don't overwrite) existing files\n"
761 " -m Don't restore modification times\n"
762 " -O Write entries to stdout, don't restore to disk\n"
763 " -p Restore permissions (including ACLs, owner, file flags)\n";
764
765
766/*
767 * Note that the word 'bsdtar' will always appear in the first line
768 * of output.
769 *
770 * In particular, /bin/sh scripts that need to test for the presence
771 * of bsdtar can use the following template:
772 *
773 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
774 * echo bsdtar; else echo not bsdtar; fi
775 */
776static void
9c82a63e 777long_help(void)
63551f52
PA
778{
779 const char *prog;
780 const char *p;
781
9c82a63e 782 prog = lafe_progname;
63551f52
PA
783
784 fflush(stderr);
785
786 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
787 printf("%s%s: manipulate archive files\n", prog, p);
788
789 for (p = long_help_msg; *p != '\0'; p++) {
790 if (*p == '%') {
791 if (p[1] == 'p') {
792 fputs(prog, stdout);
793 p++;
794 } else
795 putchar('%');
796 } else
797 putchar(*p);
798 }
799 version();
800}