bsdtar.c revision 302001
1/*-
2 * Copyright (c) 2003-2008 Tim Kientzle
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"
27__FBSDID("$FreeBSD: stable/10/contrib/libarchive/tar/bsdtar.c 302001 2016-06-17 22:40:10Z mm $");
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
35#ifdef HAVE_COPYFILE_H
36#include <copyfile.h>
37#endif
38#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
41#ifdef HAVE_FCNTL_H
42#include <fcntl.h>
43#endif
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
53#ifdef HAVE_SIGNAL_H
54#include <signal.h>
55#endif
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
69
70#include "bsdtar.h"
71#include "err.h"
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
80#if defined(_WIN32) && !defined(__CYGWIN__)
81#define	_PATH_DEFTAPE "\\\\.\\tape0"
82#endif
83#if defined(__APPLE__)
84#undef _PATH_DEFTAPE
85#define	_PATH_DEFTAPE "-"  /* Mac OS has no tape support, default to stdio. */
86#endif
87
88#ifndef _PATH_DEFTAPE
89#define	_PATH_DEFTAPE "/dev/tape"
90#endif
91
92#ifdef __MINGW32__
93int _CRT_glob = 0; /* Disable broken CRT globbing. */
94#endif
95
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
121static void		 long_help(void);
122static void		 only_mode(struct bsdtar *, const char *opt,
123			     const char *valid);
124static void		 set_mode(struct bsdtar *, char opt);
125static void		 version(void);
126
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;
136	int			 opt, t;
137	char			 compression, compression2;
138	const char		*compression_name, *compression2_name;
139	const char		*compress_program;
140	char			 option_a, option_o;
141	char			 possible_help_request;
142	char			 buff[16];
143
144	/*
145	 * Use a pointer for consistency, but stack-allocated storage
146	 * for ease of cleanup.
147	 */
148	bsdtar = &bsdtar_storage;
149	memset(bsdtar, 0, sizeof(*bsdtar));
150	bsdtar->fd = -1; /* Mark as "unused" */
151	bsdtar->gid = -1;
152	bsdtar->uid = -1;
153	option_a = option_o = 0;
154	compression = compression2 = '\0';
155	compression_name = compression2_name = NULL;
156	compress_program = NULL;
157
158#if defined(HAVE_SIGACTION)
159	{ /* Set up signal handling. */
160		struct sigaction sa;
161		sa.sa_handler = siginfo_handler;
162		sigemptyset(&sa.sa_mask);
163		sa.sa_flags = 0;
164#ifdef SIGINFO
165		if (sigaction(SIGINFO, &sa, NULL))
166			lafe_errc(1, errno, "sigaction(SIGINFO) failed");
167#endif
168#ifdef SIGUSR1
169		/* ... and treat SIGUSR1 the same way as SIGINFO. */
170		if (sigaction(SIGUSR1, &sa, NULL))
171			lafe_errc(1, errno, "sigaction(SIGUSR1) failed");
172#endif
173#ifdef SIGPIPE
174		/* Ignore SIGPIPE signals. */
175		sa.sa_handler = SIG_IGN;
176		sigaction(SIGPIPE, &sa, NULL);
177#endif
178	}
179#endif
180
181	/* Set lafe_progname before calling lafe_warnc. */
182	lafe_setprogname(*argv, "bsdtar");
183
184#if HAVE_SETLOCALE
185	if (setlocale(LC_ALL, "") == NULL)
186		lafe_warnc(0, "Failed to set default locale");
187#endif
188#if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
189	bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
190#endif
191	possible_help_request = 0;
192
193	/* Look up uid of current user for future reference */
194	bsdtar->user_uid = geteuid();
195
196	/* Default: open tape drive. */
197	bsdtar->filename = getenv("TAPE");
198	if (bsdtar->filename == NULL)
199		bsdtar->filename = _PATH_DEFTAPE;
200
201	/* Default block size settings. */
202	bsdtar->bytes_per_block = DEFAULT_BYTES_PER_BLOCK;
203	/* Allow library to default this unless user specifies -b. */
204	bsdtar->bytes_in_last_block = -1;
205
206	/* Default: preserve mod time on extract */
207	bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
208
209	/* Default: Perform basic security checks. */
210	bsdtar->extract_flags |= SECURITY;
211
212#ifndef _WIN32
213	/* On POSIX systems, assume --same-owner and -p when run by
214	 * the root user.  This doesn't make any sense on Windows. */
215	if (bsdtar->user_uid == 0) {
216		/* --same-owner */
217		bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
218		/* -p */
219		bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
220		bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
221		bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
222		bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
223		bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
224	}
225#endif
226
227	/*
228	 * Enable Mac OS "copyfile()" extension by default.
229	 * This has no effect on other platforms.
230	 */
231	bsdtar->readdisk_flags |= ARCHIVE_READDISK_MAC_COPYFILE;
232#ifdef COPYFILE_DISABLE_VAR
233	if (getenv(COPYFILE_DISABLE_VAR))
234		bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
235#endif
236	bsdtar->matching = archive_match_new();
237	if (bsdtar->matching == NULL)
238		lafe_errc(1, errno, "Out of memory");
239	bsdtar->cset = cset_new();
240	if (bsdtar->cset == NULL)
241		lafe_errc(1, errno, "Out of memory");
242
243	bsdtar->argv = argv;
244	bsdtar->argc = argc;
245
246	/*
247	 * Comments following each option indicate where that option
248	 * originated:  SUSv2, POSIX, GNU tar, star, etc.  If there's
249	 * no such comment, then I don't know of anyone else who
250	 * implements that option.
251	 */
252	while ((opt = bsdtar_getopt(bsdtar)) != -1) {
253		switch (opt) {
254		case 'a': /* GNU tar */
255			option_a = 1; /* Record it and resolve it later. */
256			break;
257		case 'B': /* GNU tar */
258			/* libarchive doesn't need this; just ignore it. */
259			break;
260		case 'b': /* SUSv2 */
261			t = atoi(bsdtar->argument);
262			if (t <= 0 || t > 8192)
263				lafe_errc(1, 0,
264				    "Argument to -b is out of range (1..8192)");
265			bsdtar->bytes_per_block = 512 * t;
266			/* Explicit -b forces last block size. */
267			bsdtar->bytes_in_last_block = bsdtar->bytes_per_block;
268			break;
269		case OPTION_B64ENCODE:
270			if (compression2 != '\0')
271				lafe_errc(1, 0,
272				    "Can't specify both --uuencode and "
273				    "--b64encode");
274			compression2 = opt;
275			compression2_name = "b64encode";
276			break;
277		case 'C': /* GNU tar */
278			if (strlen(bsdtar->argument) == 0)
279				lafe_errc(1, 0,
280				    "Meaningless option: -C ''");
281
282			set_chdir(bsdtar, bsdtar->argument);
283			break;
284		case 'c': /* SUSv2 */
285			set_mode(bsdtar, opt);
286			break;
287		case OPTION_CHECK_LINKS: /* GNU tar */
288			bsdtar->option_warn_links = 1;
289			break;
290		case OPTION_CHROOT: /* NetBSD */
291			bsdtar->option_chroot = 1;
292			break;
293		case OPTION_CLEAR_NOCHANGE_FFLAGS:
294			bsdtar->extract_flags |=
295			    ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS;
296			break;
297		case OPTION_DISABLE_COPYFILE: /* Mac OS X */
298			bsdtar->readdisk_flags &= ~ARCHIVE_READDISK_MAC_COPYFILE;
299			break;
300		case OPTION_EXCLUDE: /* GNU tar */
301			if (archive_match_exclude_pattern(
302			    bsdtar->matching, bsdtar->argument) != ARCHIVE_OK)
303				lafe_errc(1, 0,
304				    "Couldn't exclude %s\n", bsdtar->argument);
305			break;
306		case OPTION_FORMAT: /* GNU tar, others */
307			cset_set_format(bsdtar->cset, bsdtar->argument);
308			break;
309		case 'f': /* SUSv2 */
310			bsdtar->filename = bsdtar->argument;
311			break;
312		case OPTION_GID: /* cpio */
313			t = atoi(bsdtar->argument);
314			if (t < 0)
315				lafe_errc(1, 0,
316				    "Argument to --gid must be positive");
317			bsdtar->gid = t;
318			break;
319		case OPTION_GNAME: /* cpio */
320			bsdtar->gname = bsdtar->argument;
321			break;
322		case OPTION_GRZIP:
323			if (compression != '\0')
324				lafe_errc(1, 0,
325				    "Can't specify both -%c and -%c", opt,
326				    compression);
327			compression = opt;
328			compression_name = "grzip";
329			break;
330		case 'H': /* BSD convention */
331			bsdtar->symlink_mode = 'H';
332			break;
333		case 'h': /* Linux Standards Base, gtar; synonym for -L */
334			bsdtar->symlink_mode = 'L';
335			/* Hack: -h by itself is the "help" command. */
336			possible_help_request = 1;
337			break;
338		case OPTION_HELP: /* GNU tar, others */
339			long_help();
340			exit(0);
341			break;
342		case OPTION_HFS_COMPRESSION: /* Mac OS X v10.6 or later */
343			bsdtar->extract_flags |=
344			    ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED;
345			break;
346		case OPTION_IGNORE_ZEROS:
347			bsdtar->option_ignore_zeros = 1;
348			break;
349		case 'I': /* GNU tar */
350			/*
351			 * TODO: Allow 'names' to come from an archive,
352			 * not just a text file.  Design a good UI for
353			 * allowing names and mode/owner to be read
354			 * from an archive, with contents coming from
355			 * disk.  This can be used to "refresh" an
356			 * archive or to design archives with special
357			 * permissions without having to create those
358			 * permissions on disk.
359			 */
360			bsdtar->names_from_file = bsdtar->argument;
361			break;
362		case OPTION_INCLUDE:
363			/*
364			 * No one else has the @archive extension, so
365			 * no one else needs this to filter entries
366			 * when transforming archives.
367			 */
368			if (archive_match_include_pattern(bsdtar->matching,
369			    bsdtar->argument) != ARCHIVE_OK)
370				lafe_errc(1, 0,
371				    "Failed to add %s to inclusion list",
372				    bsdtar->argument);
373			break;
374		case 'j': /* GNU tar */
375			if (compression != '\0')
376				lafe_errc(1, 0,
377				    "Can't specify both -%c and -%c", opt,
378				    compression);
379			compression = opt;
380			compression_name = "bzip2";
381			break;
382		case 'J': /* GNU tar 1.21 and later */
383			if (compression != '\0')
384				lafe_errc(1, 0,
385				    "Can't specify both -%c and -%c", opt,
386				    compression);
387			compression = opt;
388			compression_name = "xz";
389			break;
390		case 'k': /* GNU tar */
391			bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
392			break;
393		case OPTION_KEEP_NEWER_FILES: /* GNU tar */
394			bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
395			break;
396		case 'L': /* BSD convention */
397			bsdtar->symlink_mode = 'L';
398			break;
399	        case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
400			/* GNU tar 1.13  used -l for --one-file-system */
401			bsdtar->option_warn_links = 1;
402			break;
403		case OPTION_LRZIP:
404		case OPTION_LZ4:
405		case OPTION_LZIP: /* GNU tar beginning with 1.23 */
406		case OPTION_LZMA: /* GNU tar beginning with 1.20 */
407		case OPTION_LZOP: /* GNU tar beginning with 1.21 */
408			if (compression != '\0')
409				lafe_errc(1, 0,
410				    "Can't specify both -%c and -%c", opt,
411				    compression);
412			compression = opt;
413			switch (opt) {
414			case OPTION_LRZIP: compression_name = "lrzip"; break;
415			case OPTION_LZ4:  compression_name = "lz4"; break;
416			case OPTION_LZIP: compression_name = "lzip"; break;
417			case OPTION_LZMA: compression_name = "lzma"; break;
418			case OPTION_LZOP: compression_name = "lzop"; break;
419			}
420			break;
421		case 'm': /* SUSv2 */
422			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
423			break;
424		case 'n': /* GNU tar */
425			bsdtar->option_no_subdirs = 1;
426			break;
427	        /*
428		 * Selecting files by time:
429		 *    --newer-?time='date' Only files newer than 'date'
430		 *    --newer-?time-than='file' Only files newer than time
431		 *         on specified file (useful for incremental backups)
432		 */
433		case OPTION_NEWER_CTIME: /* GNU tar */
434			if (archive_match_include_date(bsdtar->matching,
435			    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
436			    bsdtar->argument) != ARCHIVE_OK)
437				lafe_errc(1, 0, "Error : %s",
438				    archive_error_string(bsdtar->matching));
439			break;
440		case OPTION_NEWER_CTIME_THAN:
441			if (archive_match_include_file_time(bsdtar->matching,
442			    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_NEWER,
443			    bsdtar->argument) != ARCHIVE_OK)
444				lafe_errc(1, 0, "Error : %s",
445				    archive_error_string(bsdtar->matching));
446			break;
447		case OPTION_NEWER_MTIME: /* GNU tar */
448			if (archive_match_include_date(bsdtar->matching,
449			    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
450			    bsdtar->argument) != ARCHIVE_OK)
451				lafe_errc(1, 0, "Error : %s",
452				    archive_error_string(bsdtar->matching));
453			break;
454		case OPTION_NEWER_MTIME_THAN:
455			if (archive_match_include_file_time(bsdtar->matching,
456			    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_NEWER,
457			    bsdtar->argument) != ARCHIVE_OK)
458				lafe_errc(1, 0, "Error : %s",
459				    archive_error_string(bsdtar->matching));
460			break;
461		case OPTION_NODUMP: /* star */
462			bsdtar->readdisk_flags |= ARCHIVE_READDISK_HONOR_NODUMP;
463			break;
464		case OPTION_NOPRESERVE_HFS_COMPRESSION:
465			/* Mac OS X v10.6 or later */
466			bsdtar->extract_flags |=
467			    ARCHIVE_EXTRACT_NO_HFS_COMPRESSION;
468			break;
469		case OPTION_NO_SAME_OWNER: /* GNU tar */
470			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
471			break;
472		case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
473			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
474			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
475			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
476			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
477			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_MAC_METADATA;
478			break;
479		case OPTION_NO_XATTR: /* Issue #131 */
480			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
481			bsdtar->readdisk_flags |= ARCHIVE_READDISK_NO_XATTR;
482			break;
483		case OPTION_NULL: /* GNU tar */
484			bsdtar->option_null++;
485			break;
486		case OPTION_NUMERIC_OWNER: /* GNU tar */
487			bsdtar->uname = "";
488			bsdtar->gname = "";
489			bsdtar->option_numeric_owner++;
490			break;
491		case 'O': /* GNU tar */
492			bsdtar->option_stdout = 1;
493			break;
494		case 'o': /* SUSv2 and GNU conflict here, but not fatally */
495			option_o = 1; /* Record it and resolve it later. */
496			break;
497	        /*
498		 * Selecting files by time:
499		 *    --older-?time='date' Only files older than 'date'
500		 *    --older-?time-than='file' Only files older than time
501		 *         on specified file
502		 */
503		case OPTION_OLDER_CTIME:
504			if (archive_match_include_date(bsdtar->matching,
505			    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER,
506			    bsdtar->argument) != ARCHIVE_OK)
507				lafe_errc(1, 0, "Error : %s",
508				    archive_error_string(bsdtar->matching));
509			break;
510		case OPTION_OLDER_CTIME_THAN:
511			if (archive_match_include_file_time(bsdtar->matching,
512			    ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER,
513			    bsdtar->argument) != ARCHIVE_OK)
514				lafe_errc(1, 0, "Error : %s",
515				    archive_error_string(bsdtar->matching));
516			break;
517		case OPTION_OLDER_MTIME:
518			if (archive_match_include_date(bsdtar->matching,
519			    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER,
520			    bsdtar->argument) != ARCHIVE_OK)
521				lafe_errc(1, 0, "Error : %s",
522				    archive_error_string(bsdtar->matching));
523			break;
524		case OPTION_OLDER_MTIME_THAN:
525			if (archive_match_include_file_time(bsdtar->matching,
526			    ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER,
527			    bsdtar->argument) != ARCHIVE_OK)
528				lafe_errc(1, 0, "Error : %s",
529				    archive_error_string(bsdtar->matching));
530			break;
531		case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
532			bsdtar->readdisk_flags |=
533			    ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS;
534			break;
535		case OPTION_OPTIONS:
536			bsdtar->option_options = bsdtar->argument;
537			break;
538#if 0
539		/*
540		 * The common BSD -P option is not necessary, since
541		 * our default is to archive symlinks, not follow
542		 * them.  This is convenient, as -P conflicts with GNU
543		 * tar anyway.
544		 */
545		case 'P': /* BSD convention */
546			/* Default behavior, no option necessary. */
547			break;
548#endif
549		case 'P': /* GNU tar */
550			bsdtar->extract_flags &= ~SECURITY;
551			bsdtar->option_absolute_paths = 1;
552			break;
553		case 'p': /* GNU tar, star */
554			bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
555			bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
556			bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
557			bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
558			bsdtar->extract_flags |= ARCHIVE_EXTRACT_MAC_METADATA;
559			break;
560		case OPTION_PASSPHRASE:
561			bsdtar->passphrase = bsdtar->argument;
562			break;
563		case OPTION_POSIX: /* GNU tar */
564			cset_set_format(bsdtar->cset, "pax");
565			break;
566		case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
567			bsdtar->option_fast_read = 1;
568			break;
569		case 'r': /* SUSv2 */
570			set_mode(bsdtar, opt);
571			break;
572		case 'S': /* NetBSD pax-as-tar */
573			bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
574			break;
575		case 's': /* NetBSD pax-as-tar */
576#if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
577			add_substitution(bsdtar, bsdtar->argument);
578#else
579			lafe_warnc(0,
580			    "-s is not supported by this version of bsdtar");
581			usage();
582#endif
583			break;
584		case OPTION_SAME_OWNER: /* GNU tar */
585			bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
586			break;
587		case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
588			errno = 0;
589			bsdtar->strip_components = strtol(bsdtar->argument,
590			    NULL, 0);
591			if (errno)
592				lafe_errc(1, 0,
593				    "Invalid --strip-components argument: %s",
594				    bsdtar->argument);
595			break;
596		case 'T': /* GNU tar */
597			bsdtar->names_from_file = bsdtar->argument;
598			break;
599		case 't': /* SUSv2 */
600			set_mode(bsdtar, opt);
601			bsdtar->verbose++;
602			break;
603		case OPTION_TOTALS: /* GNU tar */
604			bsdtar->option_totals++;
605			break;
606		case 'U': /* GNU tar */
607			bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
608			bsdtar->option_unlink_first = 1;
609			break;
610		case 'u': /* SUSv2 */
611			set_mode(bsdtar, opt);
612			break;
613		case OPTION_UID: /* cpio */
614			t = atoi(bsdtar->argument);
615			if (t < 0)
616				lafe_errc(1, 0,
617				    "Argument to --uid must be positive");
618			bsdtar->uid = t;
619			break;
620		case OPTION_UNAME: /* cpio */
621			bsdtar->uname = bsdtar->argument;
622			break;
623		case OPTION_UUENCODE:
624			if (compression2 != '\0')
625				lafe_errc(1, 0,
626				    "Can't specify both --uuencode and "
627				    "--b64encode");
628			compression2 = opt;
629			compression2_name = "uuencode";
630			break;
631		case 'v': /* SUSv2 */
632			bsdtar->verbose++;
633			break;
634		case OPTION_VERSION: /* GNU convention */
635			version();
636			break;
637#if 0
638		/*
639		 * The -W longopt feature is handled inside of
640		 * bsdtar_getopt(), so -W is not available here.
641		 */
642		case 'W': /* Obscure GNU convention. */
643			break;
644#endif
645		case 'w': /* SUSv2 */
646			bsdtar->option_interactive = 1;
647			break;
648		case 'X': /* GNU tar */
649			if (archive_match_exclude_pattern_from_file(
650			    bsdtar->matching, bsdtar->argument, 0)
651			    != ARCHIVE_OK)
652				lafe_errc(1, 0, "Error : %s",
653				    archive_error_string(bsdtar->matching));
654			break;
655		case 'x': /* SUSv2 */
656			set_mode(bsdtar, opt);
657			break;
658		case 'y': /* FreeBSD version of GNU tar */
659			if (compression != '\0')
660				lafe_errc(1, 0,
661				    "Can't specify both -%c and -%c", opt,
662				    compression);
663			compression = opt;
664			compression_name = "bzip2";
665			break;
666		case 'Z': /* GNU tar */
667			if (compression != '\0')
668				lafe_errc(1, 0,
669				    "Can't specify both -%c and -%c", opt,
670				    compression);
671			compression = opt;
672			compression_name = "compress";
673			break;
674		case 'z': /* GNU tar, star, many others */
675			if (compression != '\0')
676				lafe_errc(1, 0,
677				    "Can't specify both -%c and -%c", opt,
678				    compression);
679			compression = opt;
680			compression_name = "gzip";
681			break;
682		case OPTION_USE_COMPRESS_PROGRAM:
683			compress_program = bsdtar->argument;
684			break;
685		default:
686			usage();
687		}
688	}
689
690	/*
691	 * Sanity-check options.
692	 */
693
694	/* If no "real" mode was specified, treat -h as --help. */
695	if ((bsdtar->mode == '\0') && possible_help_request) {
696		long_help();
697		exit(0);
698	}
699
700	/* Otherwise, a mode is required. */
701	if (bsdtar->mode == '\0')
702		lafe_errc(1, 0,
703		    "Must specify one of -c, -r, -t, -u, -x");
704
705	/* Check boolean options only permitted in certain modes. */
706	if (option_a)
707		only_mode(bsdtar, "-a", "c");
708	if (bsdtar->readdisk_flags & ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS)
709		only_mode(bsdtar, "--one-file-system", "cru");
710	if (bsdtar->option_fast_read)
711		only_mode(bsdtar, "--fast-read", "xt");
712	if (bsdtar->extract_flags & ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED)
713		only_mode(bsdtar, "--hfsCompression", "x");
714	if (bsdtar->extract_flags & ARCHIVE_EXTRACT_NO_HFS_COMPRESSION)
715		only_mode(bsdtar, "--nopreserveHFSCompression", "x");
716	if (bsdtar->readdisk_flags & ARCHIVE_READDISK_HONOR_NODUMP)
717		only_mode(bsdtar, "--nodump", "cru");
718	if (bsdtar->readdisk_flags & ARCHIVE_READDISK_NO_XATTR)
719		only_mode(bsdtar, "--no-xattr", "crux");
720	if (option_o > 0) {
721		switch (bsdtar->mode) {
722		case 'c':
723			/*
724			 * In GNU tar, -o means "old format."  The
725			 * "ustar" format is the closest thing
726			 * supported by libarchive.
727			 */
728			cset_set_format(bsdtar->cset, "ustar");
729			/* TODO: bsdtar->create_format = "v7"; */
730			break;
731		case 'x':
732			/* POSIX-compatible behavior. */
733			bsdtar->option_no_owner = 1;
734			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
735			break;
736		default:
737			only_mode(bsdtar, "-o", "xc");
738			break;
739		}
740	}
741	if (bsdtar->option_no_subdirs)
742		only_mode(bsdtar, "-n", "cru");
743	if (bsdtar->option_stdout)
744		only_mode(bsdtar, "-O", "xt");
745	if (bsdtar->option_unlink_first)
746		only_mode(bsdtar, "-U", "x");
747	if (bsdtar->option_warn_links)
748		only_mode(bsdtar, "--check-links", "cr");
749
750	if (option_a && cset_auto_compress(bsdtar->cset, bsdtar->filename)) {
751		/* Ignore specified compressions if auto-compress works. */
752		compression = '\0';
753		compression2 = '\0';
754	}
755	/* Check other parameters only permitted in certain modes. */
756	if (compress_program != NULL) {
757		only_mode(bsdtar, "--use-compress-program", "cxt");
758		cset_add_filter_program(bsdtar->cset, compress_program);
759		/* Ignore specified compressions. */
760		compression = '\0';
761		compression2 = '\0';
762	}
763	if (compression != '\0') {
764		switch (compression) {
765		case 'J': case 'j': case 'y': case 'Z': case 'z':
766			strcpy(buff, "-?");
767			buff[1] = compression;
768			break;
769		default:
770			strcpy(buff, "--");
771			strcat(buff, compression_name);
772			break;
773		}
774		only_mode(bsdtar, buff, "cxt");
775		cset_add_filter(bsdtar->cset, compression_name);
776	}
777	if (compression2 != '\0') {
778		strcpy(buff, "--");
779		strcat(buff, compression2_name);
780		only_mode(bsdtar, buff, "cxt");
781		cset_add_filter(bsdtar->cset, compression2_name);
782	}
783	if (cset_get_format(bsdtar->cset) != NULL)
784		only_mode(bsdtar, "--format", "cru");
785	if (bsdtar->symlink_mode != '\0') {
786		strcpy(buff, "-?");
787		buff[1] = bsdtar->symlink_mode;
788		only_mode(bsdtar, buff, "cru");
789	}
790
791	/* Filename "-" implies stdio. */
792	if (strcmp(bsdtar->filename, "-") == 0)
793		bsdtar->filename = NULL;
794
795	switch(bsdtar->mode) {
796	case 'c':
797		tar_mode_c(bsdtar);
798		break;
799	case 'r':
800		tar_mode_r(bsdtar);
801		break;
802	case 't':
803		tar_mode_t(bsdtar);
804		break;
805	case 'u':
806		tar_mode_u(bsdtar);
807		break;
808	case 'x':
809		tar_mode_x(bsdtar);
810		break;
811	}
812
813	archive_match_free(bsdtar->matching);
814#if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
815	cleanup_substitution(bsdtar);
816#endif
817	cset_free(bsdtar->cset);
818	passphrase_free(bsdtar->ppbuff);
819
820	if (bsdtar->return_value != 0)
821		lafe_warnc(0,
822		    "Error exit delayed from previous errors.");
823	return (bsdtar->return_value);
824}
825
826static void
827set_mode(struct bsdtar *bsdtar, char opt)
828{
829	if (bsdtar->mode != '\0' && bsdtar->mode != opt)
830		lafe_errc(1, 0,
831		    "Can't specify both -%c and -%c", opt, bsdtar->mode);
832	bsdtar->mode = opt;
833}
834
835/*
836 * Verify that the mode is correct.
837 */
838static void
839only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
840{
841	if (strchr(valid_modes, bsdtar->mode) == NULL)
842		lafe_errc(1, 0,
843		    "Option %s is not permitted in mode -%c",
844		    opt, bsdtar->mode);
845}
846
847
848void
849usage(void)
850{
851	const char	*p;
852
853	p = lafe_getprogname();
854
855	fprintf(stderr, "Usage:\n");
856	fprintf(stderr, "  List:    %s -tf <archive-filename>\n", p);
857	fprintf(stderr, "  Extract: %s -xf <archive-filename>\n", p);
858	fprintf(stderr, "  Create:  %s -cf <archive-filename> [filenames...]\n", p);
859	fprintf(stderr, "  Help:    %s --help\n", p);
860	exit(1);
861}
862
863static void
864version(void)
865{
866	printf("bsdtar %s - %s\n",
867	    BSDTAR_VERSION_STRING,
868	    archive_version_details());
869	exit(0);
870}
871
872static const char *long_help_msg =
873	"First option must be a mode specifier:\n"
874	"  -c Create  -r Add/Replace  -t List  -u Update  -x Extract\n"
875	"Common Options:\n"
876	"  -b #  Use # 512-byte records per I/O block\n"
877	"  -f <filename>  Location of archive (default " _PATH_DEFTAPE ")\n"
878	"  -v    Verbose\n"
879	"  -w    Interactive\n"
880	"Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
881	"  <file>, <dir>  add these items to archive\n"
882	"  -z, -j, -J, --lzma  Compress archive with gzip/bzip2/xz/lzma\n"
883	"  --format {ustar|pax|cpio|shar}  Select archive format\n"
884	"  --exclude <pattern>  Skip files that match pattern\n"
885	"  -C <dir>  Change to <dir> before processing remaining files\n"
886	"  @<archive>  Add entries from <archive> to output\n"
887	"List: %p -t [options] [<patterns>]\n"
888	"  <patterns>  If specified, list only entries that match\n"
889	"Extract: %p -x [options] [<patterns>]\n"
890	"  <patterns>  If specified, extract only entries that match\n"
891	"  -k    Keep (don't overwrite) existing files\n"
892	"  -m    Don't restore modification times\n"
893	"  -O    Write entries to stdout, don't restore to disk\n"
894	"  -p    Restore permissions (including ACLs, owner, file flags)\n";
895
896
897/*
898 * Note that the word 'bsdtar' will always appear in the first line
899 * of output.
900 *
901 * In particular, /bin/sh scripts that need to test for the presence
902 * of bsdtar can use the following template:
903 *
904 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
905 *          echo bsdtar; else echo not bsdtar; fi
906 */
907static void
908long_help(void)
909{
910	const char	*prog;
911	const char	*p;
912
913	prog = lafe_getprogname();
914
915	fflush(stderr);
916
917	p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
918	printf("%s%s: manipulate archive files\n", prog, p);
919
920	for (p = long_help_msg; *p != '\0'; p++) {
921		if (*p == '%') {
922			if (p[1] == 'p') {
923				fputs(prog, stdout);
924				p++;
925			} else
926				putchar('%');
927		} else
928			putchar(*p);
929	}
930	version();
931}
932