bsdtar.c revision 228763
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: head/contrib/libarchive/tar/bsdtar.c 228763 2011-12-21 11:13:29Z 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_ERRNO_H
36#include <errno.h>
37#endif
38#ifdef HAVE_FCNTL_H
39#include <fcntl.h>
40#endif
41#ifdef HAVE_LANGINFO_H
42#include <langinfo.h>
43#endif
44#ifdef HAVE_LOCALE_H
45#include <locale.h>
46#endif
47#ifdef HAVE_PATHS_H
48#include <paths.h>
49#endif
50#ifdef HAVE_SIGNAL_H
51#include <signal.h>
52#endif
53#include <stdio.h>
54#ifdef HAVE_STDLIB_H
55#include <stdlib.h>
56#endif
57#ifdef HAVE_STRING_H
58#include <string.h>
59#endif
60#ifdef HAVE_TIME_H
61#include <time.h>
62#endif
63#ifdef HAVE_UNISTD_H
64#include <unistd.h>
65#endif
66#if HAVE_ZLIB_H
67#include <zlib.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
84#ifndef _PATH_DEFTAPE
85#define	_PATH_DEFTAPE "/dev/tape"
86#endif
87
88#ifdef __MINGW32__
89int _CRT_glob = 0; /* Disable broken CRT globbing. */
90#endif
91
92static struct bsdtar *_bsdtar;
93
94#if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
95static volatile int siginfo_occurred;
96
97static void
98siginfo_handler(int sig)
99{
100	(void)sig; /* UNUSED */
101	siginfo_occurred = 1;
102}
103
104int
105need_report(void)
106{
107	int r = siginfo_occurred;
108	siginfo_occurred = 0;
109	return (r);
110}
111#else
112int
113need_report(void)
114{
115	return (0);
116}
117#endif
118
119/* External function to parse a date/time string */
120time_t get_date(time_t, const char *);
121
122static void		 long_help(void);
123static void		 only_mode(struct bsdtar *, const char *opt,
124			     const char *valid);
125static void		 set_mode(struct bsdtar *, char opt);
126static void		 version(void);
127
128/* A basic set of security flags to request from libarchive. */
129#define	SECURITY					\
130	(ARCHIVE_EXTRACT_SECURE_SYMLINKS		\
131	 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
132
133int
134main(int argc, char **argv)
135{
136	struct bsdtar		*bsdtar, bsdtar_storage;
137	int			 opt, t;
138	char			 option_o;
139	char			 possible_help_request;
140	char			 buff[16];
141	time_t			 now;
142
143	/*
144	 * Use a pointer for consistency, but stack-allocated storage
145	 * for ease of cleanup.
146	 */
147	_bsdtar = bsdtar = &bsdtar_storage;
148	memset(bsdtar, 0, sizeof(*bsdtar));
149	bsdtar->fd = -1; /* Mark as "unused" */
150	bsdtar->gid = -1;
151	bsdtar->uid = -1;
152	option_o = 0;
153
154#if defined(HAVE_SIGACTION) && (defined(SIGINFO) || defined(SIGUSR1))
155	{ /* Catch SIGINFO and SIGUSR1, if they exist. */
156		struct sigaction sa;
157		sa.sa_handler = siginfo_handler;
158		sigemptyset(&sa.sa_mask);
159		sa.sa_flags = 0;
160#ifdef SIGINFO
161		if (sigaction(SIGINFO, &sa, NULL))
162			lafe_errc(1, errno, "sigaction(SIGINFO) failed");
163#endif
164#ifdef SIGUSR1
165		/* ... and treat SIGUSR1 the same way as SIGINFO. */
166		if (sigaction(SIGUSR1, &sa, NULL))
167			lafe_errc(1, errno, "sigaction(SIGUSR1) failed");
168#endif
169	}
170#endif
171
172
173	/* Need lafe_progname before calling lafe_warnc. */
174	if (*argv == NULL)
175		lafe_progname = "bsdtar";
176	else {
177#if defined(_WIN32) && !defined(__CYGWIN__)
178		lafe_progname = strrchr(*argv, '\\');
179#else
180		lafe_progname = strrchr(*argv, '/');
181#endif
182		if (lafe_progname != NULL)
183			lafe_progname++;
184		else
185			lafe_progname = *argv;
186	}
187
188	time(&now);
189
190#if HAVE_SETLOCALE
191	if (setlocale(LC_ALL, "") == NULL)
192		lafe_warnc(0, "Failed to set default locale");
193#endif
194#if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
195	bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
196#endif
197	possible_help_request = 0;
198
199	/* Look up uid of current user for future reference */
200	bsdtar->user_uid = geteuid();
201
202	/* Default: open tape drive. */
203	bsdtar->filename = getenv("TAPE");
204	if (bsdtar->filename == NULL)
205		bsdtar->filename = _PATH_DEFTAPE;
206
207	/* Default: preserve mod time on extract */
208	bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
209
210	/* Default: Perform basic security checks. */
211	bsdtar->extract_flags |= SECURITY;
212
213#ifndef _WIN32
214	/* On POSIX systems, assume --same-owner and -p when run by
215	 * the root user.  This doesn't make any sense on Windows. */
216	if (bsdtar->user_uid == 0) {
217		/* --same-owner */
218		bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
219		/* -p */
220		bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
221		bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
222		bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
223		bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
224	}
225#endif
226
227	bsdtar->argv = argv;
228	bsdtar->argc = argc;
229
230	/*
231	 * Comments following each option indicate where that option
232	 * originated:  SUSv2, POSIX, GNU tar, star, etc.  If there's
233	 * no such comment, then I don't know of anyone else who
234	 * implements that option.
235	 */
236	while ((opt = bsdtar_getopt(bsdtar)) != -1) {
237		switch (opt) {
238		case 'B': /* GNU tar */
239			/* libarchive doesn't need this; just ignore it. */
240			break;
241		case 'b': /* SUSv2 */
242			t = atoi(bsdtar->optarg);
243			if (t <= 0 || t > 8192)
244				lafe_errc(1, 0,
245				    "Argument to -b is out of range (1..8192)");
246			bsdtar->bytes_per_block = 512 * t;
247			break;
248		case 'C': /* GNU tar */
249			set_chdir(bsdtar, bsdtar->optarg);
250			break;
251		case 'c': /* SUSv2 */
252			set_mode(bsdtar, opt);
253			break;
254		case OPTION_CHECK_LINKS: /* GNU tar */
255			bsdtar->option_warn_links = 1;
256			break;
257		case OPTION_CHROOT: /* NetBSD */
258			bsdtar->option_chroot = 1;
259			break;
260		case OPTION_EXCLUDE: /* GNU tar */
261			if (lafe_exclude(&bsdtar->matching, bsdtar->optarg))
262				lafe_errc(1, 0,
263				    "Couldn't exclude %s\n", bsdtar->optarg);
264			break;
265		case OPTION_FORMAT: /* GNU tar, others */
266			bsdtar->create_format = bsdtar->optarg;
267			break;
268		case 'f': /* SUSv2 */
269			bsdtar->filename = bsdtar->optarg;
270			if (strcmp(bsdtar->filename, "-") == 0)
271				bsdtar->filename = NULL;
272			break;
273		case OPTION_GID: /* cpio */
274			t = atoi(bsdtar->optarg);
275			if (t < 0)
276				lafe_errc(1, 0,
277				    "Argument to --gid must be positive");
278			bsdtar->gid = t;
279			break;
280		case OPTION_GNAME: /* cpio */
281			bsdtar->gname = bsdtar->optarg;
282			break;
283		case 'H': /* BSD convention */
284			bsdtar->symlink_mode = 'H';
285			break;
286		case 'h': /* Linux Standards Base, gtar; synonym for -L */
287			bsdtar->symlink_mode = 'L';
288			/* Hack: -h by itself is the "help" command. */
289			possible_help_request = 1;
290			break;
291		case OPTION_HELP: /* GNU tar, others */
292			long_help();
293			exit(0);
294			break;
295		case 'I': /* GNU tar */
296			/*
297			 * TODO: Allow 'names' to come from an archive,
298			 * not just a text file.  Design a good UI for
299			 * allowing names and mode/owner to be read
300			 * from an archive, with contents coming from
301			 * disk.  This can be used to "refresh" an
302			 * archive or to design archives with special
303			 * permissions without having to create those
304			 * permissions on disk.
305			 */
306			bsdtar->names_from_file = bsdtar->optarg;
307			break;
308		case OPTION_INCLUDE:
309			/*
310			 * Noone else has the @archive extension, so
311			 * noone else needs this to filter entries
312			 * when transforming archives.
313			 */
314			if (lafe_include(&bsdtar->matching, bsdtar->optarg))
315				lafe_errc(1, 0,
316				    "Failed to add %s to inclusion list",
317				    bsdtar->optarg);
318			break;
319		case 'j': /* GNU tar */
320			if (bsdtar->create_compression != '\0')
321				lafe_errc(1, 0,
322				    "Can't specify both -%c and -%c", opt,
323				    bsdtar->create_compression);
324			bsdtar->create_compression = opt;
325			break;
326		case 'J': /* GNU tar 1.21 and later */
327			if (bsdtar->create_compression != '\0')
328				lafe_errc(1, 0,
329				    "Can't specify both -%c and -%c", opt,
330				    bsdtar->create_compression);
331			bsdtar->create_compression = opt;
332			break;
333		case 'k': /* GNU tar */
334			bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
335			break;
336		case OPTION_KEEP_NEWER_FILES: /* GNU tar */
337			bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
338			break;
339		case 'L': /* BSD convention */
340			bsdtar->symlink_mode = 'L';
341			break;
342	        case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
343			/* GNU tar 1.13  used -l for --one-file-system */
344			bsdtar->option_warn_links = 1;
345			break;
346		case OPTION_LZMA:
347			if (bsdtar->create_compression != '\0')
348				lafe_errc(1, 0,
349				    "Can't specify both -%c and -%c", opt,
350				    bsdtar->create_compression);
351			bsdtar->create_compression = opt;
352			break;
353		case 'm': /* SUSv2 */
354			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
355			break;
356		case 'n': /* GNU tar */
357			bsdtar->option_no_subdirs = 1;
358			break;
359	        /*
360		 * Selecting files by time:
361		 *    --newer-?time='date' Only files newer than 'date'
362		 *    --newer-?time-than='file' Only files newer than time
363		 *         on specified file (useful for incremental backups)
364		 * TODO: Add corresponding "older" options to reverse these.
365		 */
366		case OPTION_NEWER_CTIME: /* GNU tar */
367			bsdtar->newer_ctime_sec = get_date(now, bsdtar->optarg);
368			break;
369		case OPTION_NEWER_CTIME_THAN:
370			{
371				struct stat st;
372				if (stat(bsdtar->optarg, &st) != 0)
373					lafe_errc(1, 0,
374					    "Can't open file %s", bsdtar->optarg);
375				bsdtar->newer_ctime_sec = st.st_ctime;
376				bsdtar->newer_ctime_nsec =
377				    ARCHIVE_STAT_CTIME_NANOS(&st);
378			}
379			break;
380		case OPTION_NEWER_MTIME: /* GNU tar */
381			bsdtar->newer_mtime_sec = get_date(now, bsdtar->optarg);
382			break;
383		case OPTION_NEWER_MTIME_THAN:
384			{
385				struct stat st;
386				if (stat(bsdtar->optarg, &st) != 0)
387					lafe_errc(1, 0,
388					    "Can't open file %s", bsdtar->optarg);
389				bsdtar->newer_mtime_sec = st.st_mtime;
390				bsdtar->newer_mtime_nsec =
391				    ARCHIVE_STAT_MTIME_NANOS(&st);
392			}
393			break;
394		case OPTION_NODUMP: /* star */
395			bsdtar->option_honor_nodump = 1;
396			break;
397		case OPTION_NO_SAME_OWNER: /* GNU tar */
398			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
399			break;
400		case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
401			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
402			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
403			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
404			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
405			break;
406		case OPTION_NULL: /* GNU tar */
407			bsdtar->option_null++;
408			break;
409		case OPTION_NUMERIC_OWNER: /* GNU tar */
410			bsdtar->uname = "";
411			bsdtar->gname = "";
412			break;
413		case 'O': /* GNU tar */
414			bsdtar->option_stdout = 1;
415			break;
416		case 'o': /* SUSv2 and GNU conflict here, but not fatally */
417			option_o = 1; /* Record it and resolve it later. */
418			break;
419		case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
420			bsdtar->option_dont_traverse_mounts = 1;
421			break;
422		case OPTION_OPTIONS:
423			bsdtar->option_options = bsdtar->optarg;
424			break;
425#if 0
426		/*
427		 * The common BSD -P option is not necessary, since
428		 * our default is to archive symlinks, not follow
429		 * them.  This is convenient, as -P conflicts with GNU
430		 * tar anyway.
431		 */
432		case 'P': /* BSD convention */
433			/* Default behavior, no option necessary. */
434			break;
435#endif
436		case 'P': /* GNU tar */
437			bsdtar->extract_flags &= ~SECURITY;
438			bsdtar->option_absolute_paths = 1;
439			break;
440		case 'p': /* GNU tar, star */
441			bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
442			bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
443			bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
444			bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
445			break;
446		case OPTION_POSIX: /* GNU tar */
447			bsdtar->create_format = "pax";
448			break;
449		case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
450			bsdtar->option_fast_read = 1;
451			break;
452		case 'r': /* SUSv2 */
453			set_mode(bsdtar, opt);
454			break;
455		case 'S': /* NetBSD pax-as-tar */
456			bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
457			break;
458		case 's': /* NetBSD pax-as-tar */
459#if HAVE_REGEX_H
460			add_substitution(bsdtar, bsdtar->optarg);
461#else
462			lafe_warnc(0,
463			    "-s is not supported by this version of bsdtar");
464			usage();
465#endif
466			break;
467		case OPTION_SAME_OWNER: /* GNU tar */
468			bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
469			break;
470		case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
471			bsdtar->strip_components = atoi(bsdtar->optarg);
472			break;
473		case 'T': /* GNU tar */
474			bsdtar->names_from_file = bsdtar->optarg;
475			break;
476		case 't': /* SUSv2 */
477			set_mode(bsdtar, opt);
478			bsdtar->verbose++;
479			break;
480		case OPTION_TOTALS: /* GNU tar */
481			bsdtar->option_totals++;
482			break;
483		case 'U': /* GNU tar */
484			bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
485			bsdtar->option_unlink_first = 1;
486			break;
487		case 'u': /* SUSv2 */
488			set_mode(bsdtar, opt);
489			break;
490		case OPTION_UID: /* cpio */
491			t = atoi(bsdtar->optarg);
492			if (t < 0)
493				lafe_errc(1, 0,
494				    "Argument to --uid must be positive");
495			bsdtar->uid = t;
496			break;
497		case OPTION_UNAME: /* cpio */
498			bsdtar->uname = bsdtar->optarg;
499			break;
500		case 'v': /* SUSv2 */
501			bsdtar->verbose++;
502			break;
503		case OPTION_VERSION: /* GNU convention */
504			version();
505			break;
506#if 0
507		/*
508		 * The -W longopt feature is handled inside of
509		 * bsdtar_getopt(), so -W is not available here.
510		 */
511		case 'W': /* Obscure GNU convention. */
512			break;
513#endif
514		case 'w': /* SUSv2 */
515			bsdtar->option_interactive = 1;
516			break;
517		case 'X': /* GNU tar */
518			if (lafe_exclude_from_file(&bsdtar->matching, bsdtar->optarg))
519				lafe_errc(1, 0,
520				    "failed to process exclusions from file %s",
521				    bsdtar->optarg);
522			break;
523		case 'x': /* SUSv2 */
524			set_mode(bsdtar, opt);
525			break;
526		case 'y': /* FreeBSD version of GNU tar */
527			if (bsdtar->create_compression != '\0')
528				lafe_errc(1, 0,
529				    "Can't specify both -%c and -%c", opt,
530				    bsdtar->create_compression);
531			bsdtar->create_compression = opt;
532			break;
533		case 'Z': /* GNU tar */
534			if (bsdtar->create_compression != '\0')
535				lafe_errc(1, 0,
536				    "Can't specify both -%c and -%c", opt,
537				    bsdtar->create_compression);
538			bsdtar->create_compression = opt;
539			break;
540		case 'z': /* GNU tar, star, many others */
541			if (bsdtar->create_compression != '\0')
542				lafe_errc(1, 0,
543				    "Can't specify both -%c and -%c", opt,
544				    bsdtar->create_compression);
545			bsdtar->create_compression = opt;
546			break;
547		case OPTION_USE_COMPRESS_PROGRAM:
548			bsdtar->compress_program = bsdtar->optarg;
549			break;
550		default:
551			usage();
552		}
553	}
554
555	/*
556	 * Sanity-check options.
557	 */
558
559	/* If no "real" mode was specified, treat -h as --help. */
560	if ((bsdtar->mode == '\0') && possible_help_request) {
561		long_help();
562		exit(0);
563	}
564
565	/* Otherwise, a mode is required. */
566	if (bsdtar->mode == '\0')
567		lafe_errc(1, 0,
568		    "Must specify one of -c, -r, -t, -u, -x");
569
570	/* Check boolean options only permitted in certain modes. */
571	if (bsdtar->option_dont_traverse_mounts)
572		only_mode(bsdtar, "--one-file-system", "cru");
573	if (bsdtar->option_fast_read)
574		only_mode(bsdtar, "--fast-read", "xt");
575	if (bsdtar->option_honor_nodump)
576		only_mode(bsdtar, "--nodump", "cru");
577	if (option_o > 0) {
578		switch (bsdtar->mode) {
579		case 'c':
580			/*
581			 * In GNU tar, -o means "old format."  The
582			 * "ustar" format is the closest thing
583			 * supported by libarchive.
584			 */
585			bsdtar->create_format = "ustar";
586			/* TODO: bsdtar->create_format = "v7"; */
587			break;
588		case 'x':
589			/* POSIX-compatible behavior. */
590			bsdtar->option_no_owner = 1;
591			bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
592			break;
593		default:
594			only_mode(bsdtar, "-o", "xc");
595			break;
596		}
597	}
598	if (bsdtar->option_no_subdirs)
599		only_mode(bsdtar, "-n", "cru");
600	if (bsdtar->option_stdout)
601		only_mode(bsdtar, "-O", "xt");
602	if (bsdtar->option_unlink_first)
603		only_mode(bsdtar, "-U", "x");
604	if (bsdtar->option_warn_links)
605		only_mode(bsdtar, "--check-links", "cr");
606
607	/* Check other parameters only permitted in certain modes. */
608	if (bsdtar->create_compression != '\0') {
609		strcpy(buff, "-?");
610		buff[1] = bsdtar->create_compression;
611		only_mode(bsdtar, buff, "cxt");
612	}
613	if (bsdtar->create_format != NULL)
614		only_mode(bsdtar, "--format", "cru");
615	if (bsdtar->symlink_mode != '\0') {
616		strcpy(buff, "-?");
617		buff[1] = bsdtar->symlink_mode;
618		only_mode(bsdtar, buff, "cru");
619	}
620	if (bsdtar->strip_components != 0)
621		only_mode(bsdtar, "--strip-components", "xt");
622
623	switch(bsdtar->mode) {
624	case 'c':
625		tar_mode_c(bsdtar);
626		break;
627	case 'r':
628		tar_mode_r(bsdtar);
629		break;
630	case 't':
631		tar_mode_t(bsdtar);
632		break;
633	case 'u':
634		tar_mode_u(bsdtar);
635		break;
636	case 'x':
637		tar_mode_x(bsdtar);
638		break;
639	}
640
641	lafe_cleanup_exclusions(&bsdtar->matching);
642#if HAVE_REGEX_H
643	cleanup_substitution(bsdtar);
644#endif
645
646	if (bsdtar->return_value != 0)
647		lafe_warnc(0,
648		    "Error exit delayed from previous errors.");
649	return (bsdtar->return_value);
650}
651
652static void
653set_mode(struct bsdtar *bsdtar, char opt)
654{
655	if (bsdtar->mode != '\0' && bsdtar->mode != opt)
656		lafe_errc(1, 0,
657		    "Can't specify both -%c and -%c", opt, bsdtar->mode);
658	bsdtar->mode = opt;
659}
660
661/*
662 * Verify that the mode is correct.
663 */
664static void
665only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
666{
667	if (strchr(valid_modes, bsdtar->mode) == NULL)
668		lafe_errc(1, 0,
669		    "Option %s is not permitted in mode -%c",
670		    opt, bsdtar->mode);
671}
672
673
674void
675usage(void)
676{
677	const char	*p;
678
679	p = lafe_progname;
680
681	fprintf(stderr, "Usage:\n");
682	fprintf(stderr, "  List:    %s -tf <archive-filename>\n", p);
683	fprintf(stderr, "  Extract: %s -xf <archive-filename>\n", p);
684	fprintf(stderr, "  Create:  %s -cf <archive-filename> [filenames...]\n", p);
685	fprintf(stderr, "  Help:    %s --help\n", p);
686	exit(1);
687}
688
689static void
690version(void)
691{
692	printf("bsdtar %s - %s\n",
693	    BSDTAR_VERSION_STRING,
694	    archive_version());
695	exit(0);
696}
697
698static const char *long_help_msg =
699	"First option must be a mode specifier:\n"
700	"  -c Create  -r Add/Replace  -t List  -u Update  -x Extract\n"
701	"Common Options:\n"
702	"  -b #  Use # 512-byte records per I/O block\n"
703	"  -f <filename>  Location of archive (default " _PATH_DEFTAPE ")\n"
704	"  -v    Verbose\n"
705	"  -w    Interactive\n"
706	"Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
707	"  <file>, <dir>  add these items to archive\n"
708	"  -z, -j, -J, --lzma  Compress archive with gzip/bzip2/xz/lzma\n"
709	"  --format {ustar|pax|cpio|shar}  Select archive format\n"
710	"  --exclude <pattern>  Skip files that match pattern\n"
711	"  -C <dir>  Change to <dir> before processing remaining files\n"
712	"  @<archive>  Add entries from <archive> to output\n"
713	"List: %p -t [options] [<patterns>]\n"
714	"  <patterns>  If specified, list only entries that match\n"
715	"Extract: %p -x [options] [<patterns>]\n"
716	"  <patterns>  If specified, extract only entries that match\n"
717	"  -k    Keep (don't overwrite) existing files\n"
718	"  -m    Don't restore modification times\n"
719	"  -O    Write entries to stdout, don't restore to disk\n"
720	"  -p    Restore permissions (including ACLs, owner, file flags)\n";
721
722
723/*
724 * Note that the word 'bsdtar' will always appear in the first line
725 * of output.
726 *
727 * In particular, /bin/sh scripts that need to test for the presence
728 * of bsdtar can use the following template:
729 *
730 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
731 *          echo bsdtar; else echo not bsdtar; fi
732 */
733static void
734long_help(void)
735{
736	const char	*prog;
737	const char	*p;
738
739	prog = lafe_progname;
740
741	fflush(stderr);
742
743	p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
744	printf("%s%s: manipulate archive files\n", prog, p);
745
746	for (p = long_help_msg; *p != '\0'; p++) {
747		if (*p == '%') {
748			if (p[1] == 'p') {
749				fputs(prog, stdout);
750				p++;
751			} else
752				putchar('%');
753		} else
754			putchar(*p);
755	}
756	version();
757}
758