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