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