2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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$");
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
32 #ifdef HAVE_SYS_STAT_H
41 #ifdef HAVE_GETOPT_LONG
51 #define required_argument 1
53 #ifdef HAVE_LANGINFO_H
90 * Per POSIX.1-1988, tar defaults to reading/writing archives to/from
91 * the default tape device for the system. Pick something reasonable here.
94 #define _PATH_DEFTAPE "/dev/st0"
98 #define _PATH_DEFTAPE "/dev/tape"
101 /* External function to parse a date/time string (from getdate.y) */
102 time_t get_date(const char *);
104 static int bsdtar_getopt(struct bsdtar *, const char *optstring,
105 const struct option **poption);
106 static void long_help(struct bsdtar *);
107 static void only_mode(struct bsdtar *, const char *opt,
109 static char ** rewrite_argv(struct bsdtar *,
110 int *argc, char ** src_argv,
111 const char *optstring);
112 static void set_mode(struct bsdtar *, char opt);
113 static void version(void);
116 * The leading '+' here forces the GNU version of getopt() (as well as
117 * both the GNU and BSD versions of getopt_long) to stop at the first
118 * non-option. Otherwise, GNU getopt() permutes the arguments and
119 * screws up -C processing.
121 static const char *tar_opts = "+Bb:C:cf:HhI:jkLlmnOoPprtT:UuvW:wX:xyZz";
124 * Most of these long options are deliberately not documented. They
125 * are provided only to make life easier for people who also use GNU tar.
126 * The only long options documented in the manual page are the ones
127 * with no corresponding short option, such as --exclude, --nodump,
130 * On systems that lack getopt_long, long options can be specified
131 * using -W longopt and -W longopt=value, e.g. "-W nodump" is the same
132 * as "--nodump" and "-W exclude=pattern" is the same as "--exclude
133 * pattern". This does not rely the GNU getopt() "W;" extension, so
134 * should work correctly on any system with a POSIX-compliant getopt().
137 /* Fake short equivalents for long options that otherwise lack them. */
139 OPTION_CHECK_LINKS=1,
146 OPTION_NEWER_CTIME_THAN,
148 OPTION_NEWER_MTIME_THAN,
150 OPTION_NO_SAME_OWNER,
151 OPTION_NO_SAME_PERMISSIONS,
153 OPTION_ONE_FILE_SYSTEM,
155 OPTION_STRIP_COMPONENTS,
157 OPTION_USE_COMPRESS_PROGRAM,
162 * If you add anything, be very careful to keep this list properly
163 * sorted, as the -W logic relies on it.
165 static const struct option tar_longopts[] = {
166 { "absolute-paths", no_argument, NULL, 'P' },
167 { "append", no_argument, NULL, 'r' },
168 { "block-size", required_argument, NULL, 'b' },
169 { "bunzip2", no_argument, NULL, 'j' },
170 { "bzip", no_argument, NULL, 'j' },
171 { "bzip2", no_argument, NULL, 'j' },
172 { "cd", required_argument, NULL, 'C' },
173 { "check-links", no_argument, NULL, OPTION_CHECK_LINKS },
174 { "chroot", no_argument, NULL, OPTION_CHROOT },
175 { "compress", no_argument, NULL, 'Z' },
176 { "confirmation", no_argument, NULL, 'w' },
177 { "create", no_argument, NULL, 'c' },
178 { "dereference", no_argument, NULL, 'L' },
179 { "directory", required_argument, NULL, 'C' },
180 { "exclude", required_argument, NULL, OPTION_EXCLUDE },
181 { "exclude-from", required_argument, NULL, 'X' },
182 { "extract", no_argument, NULL, 'x' },
183 { "fast-read", no_argument, NULL, 'q' },
184 { "file", required_argument, NULL, 'f' },
185 { "files-from", required_argument, NULL, 'T' },
186 { "format", required_argument, NULL, OPTION_FORMAT },
187 { "gunzip", no_argument, NULL, 'z' },
188 { "gzip", no_argument, NULL, 'z' },
189 { "help", no_argument, NULL, OPTION_HELP },
190 { "include", required_argument, NULL, OPTION_INCLUDE },
191 { "interactive", no_argument, NULL, 'w' },
192 { "insecure", no_argument, NULL, 'P' },
193 { "keep-old-files", no_argument, NULL, 'k' },
194 { "list", no_argument, NULL, 't' },
195 { "modification-time", no_argument, NULL, 'm' },
196 { "newer", required_argument, NULL, OPTION_NEWER_CTIME },
197 { "newer-ctime", required_argument, NULL, OPTION_NEWER_CTIME },
198 { "newer-ctime-than", required_argument, NULL, OPTION_NEWER_CTIME_THAN },
199 { "newer-mtime", required_argument, NULL, OPTION_NEWER_MTIME },
200 { "newer-mtime-than", required_argument, NULL, OPTION_NEWER_MTIME_THAN },
201 { "newer-than", required_argument, NULL, OPTION_NEWER_CTIME_THAN },
202 { "nodump", no_argument, NULL, OPTION_NODUMP },
203 { "norecurse", no_argument, NULL, 'n' },
204 { "no-recursion", no_argument, NULL, 'n' },
205 { "no-same-owner", no_argument, NULL, OPTION_NO_SAME_OWNER },
206 { "no-same-permissions",no_argument, NULL, OPTION_NO_SAME_PERMISSIONS },
207 { "null", no_argument, NULL, OPTION_NULL },
208 { "one-file-system", no_argument, NULL, OPTION_ONE_FILE_SYSTEM },
209 { "posix", no_argument, NULL, OPTION_POSIX },
210 { "preserve-permissions", no_argument, NULL, 'p' },
211 { "read-full-blocks", no_argument, NULL, 'B' },
212 { "same-permissions", no_argument, NULL, 'p' },
213 { "strip-components", required_argument, NULL, OPTION_STRIP_COMPONENTS },
214 { "to-stdout", no_argument, NULL, 'O' },
215 { "totals", no_argument, NULL, OPTION_TOTALS },
216 { "uncompress", no_argument, NULL, 'Z' },
217 { "unlink", no_argument, NULL, 'U' },
218 { "unlink-first", no_argument, NULL, 'U' },
219 { "update", no_argument, NULL, 'u' },
220 { "use-compress-program",
221 required_argument, NULL, OPTION_USE_COMPRESS_PROGRAM },
222 { "verbose", no_argument, NULL, 'v' },
223 { "version", no_argument, NULL, OPTION_VERSION },
227 /* A basic set of security flags to request from libarchive. */
229 (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
230 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
233 main(int argc, char **argv)
235 struct bsdtar *bsdtar, bsdtar_storage;
236 const struct option *option;
239 char possible_help_request;
243 * Use a pointer for consistency, but stack-allocated storage
244 * for ease of cleanup.
246 bsdtar = &bsdtar_storage;
247 memset(bsdtar, 0, sizeof(*bsdtar));
248 bsdtar->fd = -1; /* Mark as "unused" */
251 /* Need bsdtar->progname before calling bsdtar_warnc. */
253 bsdtar->progname = "bsdtar";
255 bsdtar->progname = strrchr(*argv, '/');
256 if (bsdtar->progname != NULL)
259 bsdtar->progname = *argv;
262 if (setlocale(LC_ALL, "") == NULL)
263 bsdtar_warnc(bsdtar, 0, "Failed to set default locale");
264 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
265 bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
267 possible_help_request = 0;
269 /* Look up uid of current user for future reference */
270 bsdtar->user_uid = geteuid();
272 /* Default: open tape drive. */
273 bsdtar->filename = getenv("TAPE");
274 if (bsdtar->filename == NULL)
275 bsdtar->filename = _PATH_DEFTAPE;
277 /* Default: preserve mod time on extract */
278 bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
280 /* Default: Perform basic security checks. */
281 bsdtar->extract_flags |= SECURITY;
283 /* Defaults for root user: */
284 if (bsdtar->user_uid == 0) {
286 bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
288 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
289 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
290 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
291 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
294 /* Rewrite traditional-style tar arguments, if used. */
295 argv = rewrite_argv(bsdtar, &argc, argv, tar_opts);
300 /* Process all remaining arguments now. */
302 * Comments following each option indicate where that option
303 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
304 * no such comment, then I don't know of anyone else who
305 * implements that option.
307 while ((opt = bsdtar_getopt(bsdtar, tar_opts, &option)) != -1) {
309 case 'B': /* GNU tar */
310 /* libarchive doesn't need this; just ignore it. */
312 case 'b': /* SUSv2 */
314 if (t <= 0 || t > 1024)
315 bsdtar_errc(bsdtar, 1, 0,
316 "Argument to -b is out of range (1..1024)");
317 bsdtar->bytes_per_block = 512 * t;
319 case 'C': /* GNU tar */
320 set_chdir(bsdtar, optarg);
322 case 'c': /* SUSv2 */
323 set_mode(bsdtar, opt);
325 case OPTION_CHECK_LINKS: /* GNU tar */
326 bsdtar->option_warn_links = 1;
328 case OPTION_CHROOT: /* NetBSD */
329 bsdtar->option_chroot = 1;
331 case OPTION_EXCLUDE: /* GNU tar */
332 if (exclude(bsdtar, optarg))
333 bsdtar_errc(bsdtar, 1, 0,
334 "Couldn't exclude %s\n", optarg);
336 case OPTION_FORMAT: /* GNU tar, others */
337 bsdtar->create_format = optarg;
339 case 'f': /* SUSv2 */
340 bsdtar->filename = optarg;
341 if (strcmp(bsdtar->filename, "-") == 0)
342 bsdtar->filename = NULL;
344 case 'H': /* BSD convention */
345 bsdtar->symlink_mode = 'H';
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;
352 case OPTION_HELP: /* GNU tar, others */
356 case 'I': /* GNU tar */
358 * TODO: Allow 'names' to come from an archive,
359 * not just a text file. Design a good UI for
360 * allowing names and mode/owner to be read
361 * from an archive, with contents coming from
362 * disk. This can be used to "refresh" an
363 * archive or to design archives with special
364 * permissions without having to create those
365 * permissions on disk.
367 bsdtar->names_from_file = optarg;
371 * Noone else has the @archive extension, so
372 * noone else needs this to filter entries
373 * when transforming archives.
375 if (include(bsdtar, optarg))
376 bsdtar_errc(bsdtar, 1, 0,
377 "Failed to add %s to inclusion list",
380 case 'j': /* GNU tar */
382 if (bsdtar->create_compression != '\0')
383 bsdtar_errc(bsdtar, 1, 0,
384 "Can't specify both -%c and -%c", opt,
385 bsdtar->create_compression);
386 bsdtar->create_compression = opt;
388 bsdtar_warnc(bsdtar, 0, "-j compression not supported by this version of bsdtar");
392 case 'k': /* GNU tar */
393 bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
395 case 'L': /* BSD convention */
396 bsdtar->symlink_mode = 'L';
398 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
399 /* GNU tar 1.13 used -l for --one-file-system */
400 bsdtar->option_warn_links = 1;
402 case 'm': /* SUSv2 */
403 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
405 case 'n': /* GNU tar */
406 bsdtar->option_no_subdirs = 1;
409 * Selecting files by time:
410 * --newer-?time='date' Only files newer than 'date'
411 * --newer-?time-than='file' Only files newer than time
412 * on specified file (useful for incremental backups)
413 * TODO: Add corresponding "older" options to reverse these.
415 case OPTION_NEWER_CTIME: /* GNU tar */
416 bsdtar->newer_ctime_sec = get_date(optarg);
418 case OPTION_NEWER_CTIME_THAN:
421 if (stat(optarg, &st) != 0)
422 bsdtar_errc(bsdtar, 1, 0,
423 "Can't open file %s", optarg);
424 bsdtar->newer_ctime_sec = st.st_ctime;
425 bsdtar->newer_ctime_nsec =
426 ARCHIVE_STAT_CTIME_NANOS(&st);
429 case OPTION_NEWER_MTIME: /* GNU tar */
430 bsdtar->newer_mtime_sec = get_date(optarg);
432 case OPTION_NEWER_MTIME_THAN:
435 if (stat(optarg, &st) != 0)
436 bsdtar_errc(bsdtar, 1, 0,
437 "Can't open file %s", optarg);
438 bsdtar->newer_mtime_sec = st.st_mtime;
439 bsdtar->newer_mtime_nsec =
440 ARCHIVE_STAT_MTIME_NANOS(&st);
443 case OPTION_NODUMP: /* star */
444 bsdtar->option_honor_nodump = 1;
446 case OPTION_NO_SAME_OWNER: /* GNU tar */
447 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
449 case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
450 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
451 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
452 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
453 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
455 case OPTION_NULL: /* GNU tar */
456 bsdtar->option_null++;
458 case 'O': /* GNU tar */
459 bsdtar->option_stdout = 1;
461 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
462 option_o = 1; /* Record it and resolve it later. */
464 case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
465 bsdtar->option_dont_traverse_mounts = 1;
469 * The common BSD -P option is not necessary, since
470 * our default is to archive symlinks, not follow
471 * them. This is convenient, as -P conflicts with GNU
474 case 'P': /* BSD convention */
475 /* Default behavior, no option necessary. */
478 case 'P': /* GNU tar */
479 bsdtar->extract_flags &= ~SECURITY;
480 bsdtar->option_absolute_paths = 1;
482 case 'p': /* GNU tar, star */
483 bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
484 bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
485 bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
486 bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
488 case OPTION_POSIX: /* GNU tar */
489 bsdtar->create_format = "pax";
491 case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
492 bsdtar->option_fast_read = 1;
494 case 'r': /* SUSv2 */
495 set_mode(bsdtar, opt);
497 case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
498 bsdtar->strip_components = atoi(optarg);
500 case 'T': /* GNU tar */
501 bsdtar->names_from_file = optarg;
503 case 't': /* SUSv2 */
504 set_mode(bsdtar, opt);
507 case OPTION_TOTALS: /* GNU tar */
508 bsdtar->option_totals++;
510 case 'U': /* GNU tar */
511 bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
512 bsdtar->option_unlink_first = 1;
514 case 'u': /* SUSv2 */
515 set_mode(bsdtar, opt);
517 case 'v': /* SUSv2 */
520 case OPTION_VERSION: /* GNU convention */
525 * The -W longopt feature is handled inside of
526 * bsdtar_getop(), so -W is not available here.
528 case 'W': /* Obscure, but useful GNU convention. */
531 case 'w': /* SUSv2 */
532 bsdtar->option_interactive = 1;
534 case 'X': /* GNU tar */
535 if (exclude_from_file(bsdtar, optarg))
536 bsdtar_errc(bsdtar, 1, 0,
537 "failed to process exclusions from file %s",
540 case 'x': /* SUSv2 */
541 set_mode(bsdtar, opt);
543 case 'y': /* FreeBSD version of GNU tar */
545 if (bsdtar->create_compression != '\0')
546 bsdtar_errc(bsdtar, 1, 0,
547 "Can't specify both -%c and -%c", opt,
548 bsdtar->create_compression);
549 bsdtar->create_compression = opt;
551 bsdtar_warnc(bsdtar, 0, "-y compression not supported by this version of bsdtar");
555 case 'Z': /* GNU tar */
556 if (bsdtar->create_compression != '\0')
557 bsdtar_errc(bsdtar, 1, 0,
558 "Can't specify both -%c and -%c", opt,
559 bsdtar->create_compression);
560 bsdtar->create_compression = opt;
562 case 'z': /* GNU tar, star, many others */
564 if (bsdtar->create_compression != '\0')
565 bsdtar_errc(bsdtar, 1, 0,
566 "Can't specify both -%c and -%c", opt,
567 bsdtar->create_compression);
568 bsdtar->create_compression = opt;
570 bsdtar_warnc(bsdtar, 0, "-z compression not supported by this version of bsdtar");
574 case OPTION_USE_COMPRESS_PROGRAM:
575 bsdtar->compress_program = optarg;
583 * Sanity-check options.
586 /* If no "real" mode was specified, treat -h as --help. */
587 if ((bsdtar->mode == '\0') && possible_help_request) {
592 /* Otherwise, a mode is required. */
593 if (bsdtar->mode == '\0')
594 bsdtar_errc(bsdtar, 1, 0,
595 "Must specify one of -c, -r, -t, -u, -x");
597 /* Check boolean options only permitted in certain modes. */
598 if (bsdtar->option_dont_traverse_mounts)
599 only_mode(bsdtar, "--one-file-system", "cru");
600 if (bsdtar->option_fast_read)
601 only_mode(bsdtar, "--fast-read", "xt");
602 if (bsdtar->option_honor_nodump)
603 only_mode(bsdtar, "--nodump", "cru");
605 switch (bsdtar->mode) {
608 * In GNU tar, -o means "old format." The
609 * "ustar" format is the closest thing
610 * supported by libarchive.
612 bsdtar->create_format = "ustar";
613 /* TODO: bsdtar->create_format = "v7"; */
616 /* POSIX-compatible behavior. */
617 bsdtar->option_no_owner = 1;
618 bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
621 only_mode(bsdtar, "-o", "xc");
625 if (bsdtar->option_no_subdirs)
626 only_mode(bsdtar, "-n", "cru");
627 if (bsdtar->option_stdout)
628 only_mode(bsdtar, "-O", "xt");
629 if (bsdtar->option_unlink_first)
630 only_mode(bsdtar, "-U", "x");
631 if (bsdtar->option_warn_links)
632 only_mode(bsdtar, "--check-links", "cr");
634 if (bsdtar->create_compression != '\0') {
636 buff[1] = bsdtar->create_compression;
637 only_mode(bsdtar, buff, "cxt");
639 if (bsdtar->create_format != NULL)
640 only_mode(bsdtar, "--format", "c");
641 if (bsdtar->symlink_mode != '\0') {
643 buff[1] = bsdtar->symlink_mode;
644 only_mode(bsdtar, buff, "cru");
646 if (bsdtar->strip_components != 0)
647 only_mode(bsdtar, "--strip-components", "xt");
649 bsdtar->argc -= optind;
650 bsdtar->argv += optind;
652 switch(bsdtar->mode) {
670 cleanup_exclusions(bsdtar);
671 if (bsdtar->return_value != 0)
672 bsdtar_warnc(bsdtar, 0,
673 "Error exit delayed from previous errors.");
674 return (bsdtar->return_value);
678 set_mode(struct bsdtar *bsdtar, char opt)
680 if (bsdtar->mode != '\0' && bsdtar->mode != opt)
681 bsdtar_errc(bsdtar, 1, 0,
682 "Can't specify both -%c and -%c", opt, bsdtar->mode);
687 * Verify that the mode is correct.
690 only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
692 if (strchr(valid_modes, bsdtar->mode) == NULL)
693 bsdtar_errc(bsdtar, 1, 0,
694 "Option %s is not permitted in mode -%c",
700 * Convert traditional tar arguments into new-style.
702 * tar tvfb file.tar 32 --exclude FOO
703 * will be converted to
704 * tar -t -v -f file.tar -b 32 --exclude FOO
706 * This requires building a new argv array. The initial bundled word
707 * gets expanded into a new string that looks like "-t\0-v\0-f\0-b\0".
708 * The new argv array has pointers into this string intermingled with
709 * pointers to the existing arguments. Arguments are moved to
710 * immediately follow their options.
712 * The optstring argument here is the same one passed to getopt(3).
713 * It is used to determine which option letters have trailing arguments.
716 rewrite_argv(struct bsdtar *bsdtar, int *argc, char **src_argv,
717 const char *optstring)
719 char **new_argv, **dest_argv;
723 if (src_argv[0] == NULL ||
724 src_argv[1] == NULL || src_argv[1][0] == '-')
727 *argc += strlen(src_argv[1]) - 1;
728 new_argv = malloc((*argc + 1) * sizeof(new_argv[0]));
729 if (new_argv == NULL)
730 bsdtar_errc(bsdtar, 1, errno, "No Memory");
732 dest_argv = new_argv;
733 *dest_argv++ = *src_argv++;
735 dest = malloc(strlen(*src_argv) * 3);
737 bsdtar_errc(bsdtar, 1, errno, "No memory");
738 for (src = *src_argv++; *src != '\0'; src++) {
743 /* If option takes an argument, insert that into the list. */
744 for (p = optstring; p != NULL && *p != '\0'; p++) {
747 if (p[1] != ':') /* No arg required, done. */
749 if (*src_argv == NULL) /* No arg available? Error. */
750 bsdtar_errc(bsdtar, 1, 0,
751 "Option %c requires an argument",
753 *dest_argv++ = *src_argv++;
758 /* Copy remaining arguments, including trailing NULL. */
759 while ((*dest_argv++ = *src_argv++) != NULL)
766 usage(struct bsdtar *bsdtar)
770 p = bsdtar->progname;
772 fprintf(stderr, "Usage:\n");
773 fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
774 fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
775 fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
776 #ifdef HAVE_GETOPT_LONG
777 fprintf(stderr, " Help: %s --help\n", p);
779 fprintf(stderr, " Help: %s -h\n", p);
787 printf("bsdtar %s - %s\n",
788 BSDTAR_VERSION_STRING,
793 static const char *long_help_msg =
794 "First option must be a mode specifier:\n"
795 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
797 " -b # Use # 512-byte records per I/O block\n"
798 " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
801 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
802 " <file>, <dir> add these items to archive\n"
803 " -z, -j Compress archive with gzip/bzip2\n"
804 " --format {ustar|pax|cpio|shar} Select archive format\n"
805 #ifdef HAVE_GETOPT_LONG
806 " --exclude <pattern> Skip files that match pattern\n"
808 " -W exclude=<pattern> Skip files that match pattern\n"
810 " -C <dir> Change to <dir> before processing remaining files\n"
811 " @<archive> Add entries from <archive> to output\n"
812 "List: %p -t [options] [<patterns>]\n"
813 " <patterns> If specified, list only entries that match\n"
814 "Extract: %p -x [options] [<patterns>]\n"
815 " <patterns> If specified, extract only entries that match\n"
816 " -k Keep (don't overwrite) existing files\n"
817 " -m Don't restore modification times\n"
818 " -O Write entries to stdout, don't restore to disk\n"
819 " -p Restore permissions (including ACLs, owner, file flags)\n";
823 * Note that the word 'bsdtar' will always appear in the first line
826 * In particular, /bin/sh scripts that need to test for the presence
827 * of bsdtar can use the following template:
829 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
830 * echo bsdtar; else echo not bsdtar; fi
833 long_help(struct bsdtar *bsdtar)
838 prog = bsdtar->progname;
842 p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
843 printf("%s%s: manipulate archive files\n", prog, p);
845 for (p = long_help_msg; *p != '\0'; p++) {
859 bsdtar_getopt(struct bsdtar *bsdtar, const char *optstring,
860 const struct option **poption)
863 const struct option *option;
866 size_t option_length;
871 #ifdef HAVE_GETOPT_LONG
872 opt = getopt_long(bsdtar->argc, bsdtar->argv, optstring,
873 tar_longopts, &option_index);
874 if (option_index > -1)
875 *poption = tar_longopts + option_index;
877 opt = getopt(bsdtar->argc, bsdtar->argv, optstring);
880 /* Support long options through -W longopt=value */
883 q = strchr(optarg, '=');
885 option_length = (size_t)(q - p);
888 option_length = strlen(p);
891 option = tar_longopts;
892 while (option->name != NULL &&
893 (strlen(option->name) < option_length ||
894 strncmp(p, option->name, option_length) != 0 )) {
898 if (option->name != NULL) {
902 /* If the first match was exact, we're done. */
903 if (strncmp(p, option->name, strlen(option->name)) == 0) {
904 while (option->name != NULL)
907 /* Check if there's another match. */
909 while (option->name != NULL &&
910 (strlen(option->name) < option_length ||
911 strncmp(p, option->name, option_length) != 0)) {
915 if (option->name != NULL)
916 bsdtar_errc(bsdtar, 1, 0,
917 "Ambiguous option %s "
918 "(matches both %s and %s)",
919 p, (*poption)->name, option->name);
921 if ((*poption)->has_arg == required_argument
923 bsdtar_errc(bsdtar, 1, 0,
924 "Option \"%s\" requires argument", p);
927 /* TODO: Set up a fake 'struct option' for
928 * error reporting... ? ? ? */