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