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: src/usr.bin/tar/bsdtar.c,v 1.79 2008/01/22 07:23:44 kientzle Exp $");
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 { "confirmation", no_argument
, NULL
, 'w' },
175 { "create", no_argument
, NULL
, 'c' },
176 { "dereference", no_argument
, NULL
, 'L' },
177 { "directory", required_argument
, NULL
, 'C' },
178 { "exclude", required_argument
, NULL
, OPTION_EXCLUDE
},
179 { "exclude-from", required_argument
, NULL
, 'X' },
180 { "extract", no_argument
, NULL
, 'x' },
181 { "fast-read", no_argument
, NULL
, OPTION_FAST_READ
},
182 { "file", required_argument
, NULL
, 'f' },
183 { "files-from", required_argument
, NULL
, 'T' },
184 { "format", required_argument
, NULL
, OPTION_FORMAT
},
185 { "gunzip", no_argument
, NULL
, 'z' },
186 { "gzip", no_argument
, NULL
, 'z' },
187 { "help", no_argument
, NULL
, OPTION_HELP
},
188 { "include", required_argument
, NULL
, OPTION_INCLUDE
},
189 { "interactive", no_argument
, NULL
, 'w' },
190 { "keep-old-files", no_argument
, NULL
, 'k' },
191 { "list", no_argument
, NULL
, 't' },
192 { "modification-time", no_argument
, NULL
, 'm' },
193 { "newer", required_argument
, NULL
, OPTION_NEWER_CTIME
},
194 { "newer-ctime", required_argument
, NULL
, OPTION_NEWER_CTIME
},
195 { "newer-ctime-than", required_argument
, NULL
, OPTION_NEWER_CTIME_THAN
},
196 { "newer-mtime", required_argument
, NULL
, OPTION_NEWER_MTIME
},
197 { "newer-mtime-than", required_argument
, NULL
, OPTION_NEWER_MTIME_THAN
},
198 { "newer-than", required_argument
, NULL
, OPTION_NEWER_CTIME_THAN
},
199 { "nodump", no_argument
, NULL
, OPTION_NODUMP
},
200 { "norecurse", no_argument
, NULL
, 'n' },
201 { "no-recursion", no_argument
, NULL
, 'n' },
202 { "no-same-owner", no_argument
, NULL
, OPTION_NO_SAME_OWNER
},
203 { "no-same-permissions",no_argument
, NULL
, OPTION_NO_SAME_PERMISSIONS
},
204 { "null", no_argument
, NULL
, OPTION_NULL
},
205 { "one-file-system", no_argument
, NULL
, OPTION_ONE_FILE_SYSTEM
},
206 { "posix", no_argument
, NULL
, OPTION_POSIX
},
207 { "preserve-permissions", no_argument
, NULL
, 'p' },
208 { "read-full-blocks", no_argument
, NULL
, 'B' },
209 { "same-permissions", no_argument
, NULL
, 'p' },
210 { "strip-components", required_argument
, NULL
, OPTION_STRIP_COMPONENTS
},
211 { "to-stdout", no_argument
, NULL
, 'O' },
212 { "totals", no_argument
, NULL
, OPTION_TOTALS
},
213 { "unlink", no_argument
, NULL
, 'U' },
214 { "unlink-first", no_argument
, NULL
, 'U' },
215 { "update", no_argument
, NULL
, 'u' },
216 { "use-compress-program",
217 required_argument
, NULL
, OPTION_USE_COMPRESS_PROGRAM
},
218 { "verbose", no_argument
, NULL
, 'v' },
219 { "version", no_argument
, NULL
, OPTION_VERSION
},
223 /* A basic set of security flags to request from libarchive. */
225 (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
226 | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
229 main(int argc
, char **argv
)
231 struct bsdtar
*bsdtar
, bsdtar_storage
;
232 const struct option
*option
;
235 char possible_help_request
;
239 * Use a pointer for consistency, but stack-allocated storage
240 * for ease of cleanup.
242 bsdtar
= &bsdtar_storage
;
243 memset(bsdtar
, 0, sizeof(*bsdtar
));
244 bsdtar
->fd
= -1; /* Mark as "unused" */
247 /* Need bsdtar->progname before calling bsdtar_warnc. */
249 bsdtar
->progname
= "bsdtar";
251 bsdtar
->progname
= strrchr(*argv
, '/');
252 if (bsdtar
->progname
!= NULL
)
255 bsdtar
->progname
= *argv
;
258 if (setlocale(LC_ALL
, "") == NULL
)
259 bsdtar_warnc(bsdtar
, 0, "Failed to set default locale");
260 #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
261 bsdtar
->day_first
= (*nl_langinfo(D_MD_ORDER
) == 'd');
263 possible_help_request
= 0;
265 /* Look up uid of current user for future reference */
266 bsdtar
->user_uid
= geteuid();
268 /* Default: open tape drive. */
269 bsdtar
->filename
= getenv("TAPE");
270 if (bsdtar
->filename
== NULL
)
271 bsdtar
->filename
= _PATH_DEFTAPE
;
273 /* Default: preserve mod time on extract */
274 bsdtar
->extract_flags
= ARCHIVE_EXTRACT_TIME
;
276 /* Default: Perform basic security checks. */
277 bsdtar
->extract_flags
|= SECURITY
;
279 /* Defaults for root user: */
280 if (bsdtar
->user_uid
== 0) {
282 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_OWNER
;
284 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_PERM
;
285 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_ACL
;
286 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_XATTR
;
287 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_FFLAGS
;
290 /* Rewrite traditional-style tar arguments, if used. */
291 argv
= rewrite_argv(bsdtar
, &argc
, argv
, tar_opts
);
296 /* Process all remaining arguments now. */
298 * Comments following each option indicate where that option
299 * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
300 * no such comment, then I don't know of anyone else who
301 * implements that option.
303 while ((opt
= bsdtar_getopt(bsdtar
, tar_opts
, &option
)) != -1) {
305 case 'B': /* GNU tar */
306 /* libarchive doesn't need this; just ignore it. */
308 case 'b': /* SUSv2 */
310 if (t
<= 0 || t
> 1024)
311 bsdtar_errc(bsdtar
, 1, 0,
312 "Argument to -b is out of range (1..1024)");
313 bsdtar
->bytes_per_block
= 512 * t
;
315 case 'C': /* GNU tar */
316 set_chdir(bsdtar
, optarg
);
318 case 'c': /* SUSv2 */
319 set_mode(bsdtar
, opt
);
321 case OPTION_CHECK_LINKS
: /* GNU tar */
322 bsdtar
->option_warn_links
= 1;
324 case OPTION_EXCLUDE
: /* GNU tar */
325 if (exclude(bsdtar
, optarg
))
326 bsdtar_errc(bsdtar
, 1, 0,
327 "Couldn't exclude %s\n", optarg
);
329 case OPTION_FORMAT
: /* GNU tar, others */
330 bsdtar
->create_format
= optarg
;
332 case 'f': /* SUSv2 */
333 bsdtar
->filename
= optarg
;
334 if (strcmp(bsdtar
->filename
, "-") == 0)
335 bsdtar
->filename
= NULL
;
337 case OPTION_FAST_READ
: /* GNU tar */
338 bsdtar
->option_fast_read
= 1;
340 case 'H': /* BSD convention */
341 bsdtar
->symlink_mode
= 'H';
343 case 'h': /* Linux Standards Base, gtar; synonym for -L */
344 bsdtar
->symlink_mode
= 'L';
345 /* Hack: -h by itself is the "help" command. */
346 possible_help_request
= 1;
348 case OPTION_HELP
: /* GNU tar, others */
352 case 'I': /* GNU tar */
354 * TODO: Allow 'names' to come from an archive,
355 * not just a text file. Design a good UI for
356 * allowing names and mode/owner to be read
357 * from an archive, with contents coming from
358 * disk. This can be used to "refresh" an
359 * archive or to design archives with special
360 * permissions without having to create those
361 * permissions on disk.
363 bsdtar
->names_from_file
= optarg
;
367 * Noone else has the @archive extension, so
368 * noone else needs this to filter entries
369 * when transforming archives.
371 if (include(bsdtar
, optarg
))
372 bsdtar_errc(bsdtar
, 1, 0,
373 "Failed to add %s to inclusion list",
376 case 'j': /* GNU tar */
378 if (bsdtar
->create_compression
!= '\0')
379 bsdtar_errc(bsdtar
, 1, 0,
380 "Can't specify both -%c and -%c", opt
,
381 bsdtar
->create_compression
);
382 bsdtar
->create_compression
= opt
;
384 bsdtar_warnc(bsdtar
, 0, "-j compression not supported by this version of bsdtar");
388 case 'k': /* GNU tar */
389 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_NO_OVERWRITE
;
391 case 'L': /* BSD convention */
392 bsdtar
->symlink_mode
= 'L';
394 case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
395 /* GNU tar 1.13 used -l for --one-file-system */
396 bsdtar
->option_warn_links
= 1;
398 case 'm': /* SUSv2 */
399 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_TIME
;
401 case 'n': /* GNU tar */
402 bsdtar
->option_no_subdirs
= 1;
405 * Selecting files by time:
406 * --newer-?time='date' Only files newer than 'date'
407 * --newer-?time-than='file' Only files newer than time
408 * on specified file (useful for incremental backups)
409 * TODO: Add corresponding "older" options to reverse these.
411 case OPTION_NEWER_CTIME
: /* GNU tar */
412 bsdtar
->newer_ctime_sec
= get_date(optarg
);
414 case OPTION_NEWER_CTIME_THAN
:
417 if (stat(optarg
, &st
) != 0)
418 bsdtar_errc(bsdtar
, 1, 0,
419 "Can't open file %s", optarg
);
420 bsdtar
->newer_ctime_sec
= st
.st_ctime
;
421 bsdtar
->newer_ctime_nsec
=
422 ARCHIVE_STAT_CTIME_NANOS(&st
);
425 case OPTION_NEWER_MTIME
: /* GNU tar */
426 bsdtar
->newer_mtime_sec
= get_date(optarg
);
428 case OPTION_NEWER_MTIME_THAN
:
431 if (stat(optarg
, &st
) != 0)
432 bsdtar_errc(bsdtar
, 1, 0,
433 "Can't open file %s", optarg
);
434 bsdtar
->newer_mtime_sec
= st
.st_mtime
;
435 bsdtar
->newer_mtime_nsec
=
436 ARCHIVE_STAT_MTIME_NANOS(&st
);
439 case OPTION_NODUMP
: /* star */
440 bsdtar
->option_honor_nodump
= 1;
442 case OPTION_NO_SAME_OWNER
: /* GNU tar */
443 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_OWNER
;
445 case OPTION_NO_SAME_PERMISSIONS
: /* GNU tar */
446 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_PERM
;
447 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_ACL
;
448 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_XATTR
;
449 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_FFLAGS
;
451 case OPTION_NULL
: /* GNU tar */
452 bsdtar
->option_null
++;
454 case 'O': /* GNU tar */
455 bsdtar
->option_stdout
= 1;
457 case 'o': /* SUSv2 and GNU conflict here, but not fatally */
458 option_o
= 1; /* Record it and resolve it later. */
460 case OPTION_ONE_FILE_SYSTEM
: /* GNU tar */
461 bsdtar
->option_dont_traverse_mounts
= 1;
465 * The common BSD -P option is not necessary, since
466 * our default is to archive symlinks, not follow
467 * them. This is convenient, as -P conflicts with GNU
470 case 'P': /* BSD convention */
471 /* Default behavior, no option necessary. */
474 case 'P': /* GNU tar */
475 bsdtar
->extract_flags
&= ~SECURITY
;
476 bsdtar
->option_absolute_paths
= 1;
478 case 'p': /* GNU tar, star */
479 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_PERM
;
480 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_ACL
;
481 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_XATTR
;
482 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_FFLAGS
;
484 case OPTION_POSIX
: /* GNU tar */
485 bsdtar
->create_format
= "pax";
487 case 'r': /* SUSv2 */
488 set_mode(bsdtar
, opt
);
490 case OPTION_STRIP_COMPONENTS
: /* GNU tar 1.15 */
491 bsdtar
->strip_components
= atoi(optarg
);
493 case 'T': /* GNU tar */
494 bsdtar
->names_from_file
= optarg
;
496 case 't': /* SUSv2 */
497 set_mode(bsdtar
, opt
);
500 case OPTION_TOTALS
: /* GNU tar */
501 bsdtar
->option_totals
++;
503 case 'U': /* GNU tar */
504 bsdtar
->extract_flags
|= ARCHIVE_EXTRACT_UNLINK
;
505 bsdtar
->option_unlink_first
= 1;
507 case 'u': /* SUSv2 */
508 set_mode(bsdtar
, opt
);
510 case 'v': /* SUSv2 */
513 case OPTION_VERSION
: /* GNU convention */
518 * The -W longopt feature is handled inside of
519 * bsdtar_getop(), so -W is not available here.
521 case 'W': /* Obscure, but useful GNU convention. */
524 case 'w': /* SUSv2 */
525 bsdtar
->option_interactive
= 1;
527 case 'X': /* GNU tar */
528 if (exclude_from_file(bsdtar
, optarg
))
529 bsdtar_errc(bsdtar
, 1, 0,
530 "failed to process exclusions from file %s",
533 case 'x': /* SUSv2 */
534 set_mode(bsdtar
, opt
);
536 case 'y': /* FreeBSD version of GNU tar */
538 if (bsdtar
->create_compression
!= '\0')
539 bsdtar_errc(bsdtar
, 1, 0,
540 "Can't specify both -%c and -%c", opt
,
541 bsdtar
->create_compression
);
542 bsdtar
->create_compression
= opt
;
544 bsdtar_warnc(bsdtar
, 0, "-y compression not supported by this version of bsdtar");
548 case 'Z': /* GNU tar */
549 if (bsdtar
->create_compression
!= '\0')
550 bsdtar_errc(bsdtar
, 1, 0,
551 "Can't specify both -%c and -%c", opt
,
552 bsdtar
->create_compression
);
553 bsdtar
->create_compression
= opt
;
555 case 'z': /* GNU tar, star, many others */
557 if (bsdtar
->create_compression
!= '\0')
558 bsdtar_errc(bsdtar
, 1, 0,
559 "Can't specify both -%c and -%c", opt
,
560 bsdtar
->create_compression
);
561 bsdtar
->create_compression
= opt
;
563 bsdtar_warnc(bsdtar
, 0, "-z compression not supported by this version of bsdtar");
567 case OPTION_USE_COMPRESS_PROGRAM
:
568 bsdtar
->compress_program
= optarg
;
576 * Sanity-check options.
579 /* If no "real" mode was specified, treat -h as --help. */
580 if ((bsdtar
->mode
== '\0') && possible_help_request
) {
585 /* Otherwise, a mode is required. */
586 if (bsdtar
->mode
== '\0')
587 bsdtar_errc(bsdtar
, 1, 0,
588 "Must specify one of -c, -r, -t, -u, -x");
590 /* Check boolean options only permitted in certain modes. */
591 if (bsdtar
->option_dont_traverse_mounts
)
592 only_mode(bsdtar
, "--one-file-system", "cru");
593 if (bsdtar
->option_fast_read
)
594 only_mode(bsdtar
, "--fast-read", "xt");
595 if (bsdtar
->option_honor_nodump
)
596 only_mode(bsdtar
, "--nodump", "cru");
598 switch (bsdtar
->mode
) {
601 * In GNU tar, -o means "old format." The
602 * "ustar" format is the closest thing
603 * supported by libarchive.
605 bsdtar
->create_format
= "ustar";
606 /* TODO: bsdtar->create_format = "v7"; */
609 /* POSIX-compatible behavior. */
610 bsdtar
->option_no_owner
= 1;
611 bsdtar
->extract_flags
&= ~ARCHIVE_EXTRACT_OWNER
;
614 only_mode(bsdtar
, "-o", "xc");
618 if (bsdtar
->option_no_subdirs
)
619 only_mode(bsdtar
, "-n", "cru");
620 if (bsdtar
->option_stdout
)
621 only_mode(bsdtar
, "-O", "xt");
622 if (bsdtar
->option_unlink_first
)
623 only_mode(bsdtar
, "-U", "x");
624 if (bsdtar
->option_warn_links
)
625 only_mode(bsdtar
, "--check-links", "cr");
627 /* Check other parameters only permitted in certain modes. */
628 if (bsdtar
->create_compression
== 'Z' && bsdtar
->mode
== 'c') {
629 bsdtar_warnc(bsdtar
, 0, ".Z compression not supported");
632 if (bsdtar
->create_compression
!= '\0') {
634 buff
[1] = bsdtar
->create_compression
;
635 only_mode(bsdtar
, buff
, "cxt");
637 if (bsdtar
->create_format
!= NULL
)
638 only_mode(bsdtar
, "--format", "c");
639 if (bsdtar
->symlink_mode
!= '\0') {
641 buff
[1] = bsdtar
->symlink_mode
;
642 only_mode(bsdtar
, buff
, "cru");
644 if (bsdtar
->strip_components
!= 0)
645 only_mode(bsdtar
, "--strip-components", "xt");
647 bsdtar
->argc
-= optind
;
648 bsdtar
->argv
+= optind
;
650 switch(bsdtar
->mode
) {
668 cleanup_exclusions(bsdtar
);
669 if (bsdtar
->return_value
!= 0)
670 bsdtar_warnc(bsdtar
, 0,
671 "Error exit delayed from previous errors.");
672 return (bsdtar
->return_value
);
676 set_mode(struct bsdtar
*bsdtar
, char opt
)
678 if (bsdtar
->mode
!= '\0' && bsdtar
->mode
!= opt
)
679 bsdtar_errc(bsdtar
, 1, 0,
680 "Can't specify both -%c and -%c", opt
, bsdtar
->mode
);
685 * Verify that the mode is correct.
688 only_mode(struct bsdtar
*bsdtar
, const char *opt
, const char *valid_modes
)
690 if (strchr(valid_modes
, bsdtar
->mode
) == NULL
)
691 bsdtar_errc(bsdtar
, 1, 0,
692 "Option %s is not permitted in mode -%c",
698 * Convert traditional tar arguments into new-style.
700 * tar tvfb file.tar 32 --exclude FOO
701 * will be converted to
702 * tar -t -v -f file.tar -b 32 --exclude FOO
704 * This requires building a new argv array. The initial bundled word
705 * gets expanded into a new string that looks like "-t\0-v\0-f\0-b\0".
706 * The new argv array has pointers into this string intermingled with
707 * pointers to the existing arguments. Arguments are moved to
708 * immediately follow their options.
710 * The optstring argument here is the same one passed to getopt(3).
711 * It is used to determine which option letters have trailing arguments.
714 rewrite_argv(struct bsdtar
*bsdtar
, int *argc
, char **src_argv
,
715 const char *optstring
)
717 char **new_argv
, **dest_argv
;
721 if (src_argv
[0] == NULL
||
722 src_argv
[1] == NULL
|| src_argv
[1][0] == '-')
725 *argc
+= strlen(src_argv
[1]) - 1;
726 new_argv
= malloc((*argc
+ 1) * sizeof(new_argv
[0]));
727 if (new_argv
== NULL
)
728 bsdtar_errc(bsdtar
, 1, errno
, "No Memory");
730 dest_argv
= new_argv
;
731 *dest_argv
++ = *src_argv
++;
733 dest
= malloc(strlen(*src_argv
) * 3);
735 bsdtar_errc(bsdtar
, 1, errno
, "No memory");
736 for (src
= *src_argv
++; *src
!= '\0'; src
++) {
741 /* If option takes an argument, insert that into the list. */
742 for (p
= optstring
; p
!= NULL
&& *p
!= '\0'; p
++) {
745 if (p
[1] != ':') /* No arg required, done. */
747 if (*src_argv
== NULL
) /* No arg available? Error. */
748 bsdtar_errc(bsdtar
, 1, 0,
749 "Option %c requires an argument",
751 *dest_argv
++ = *src_argv
++;
756 /* Copy remaining arguments, including trailing NULL. */
757 while ((*dest_argv
++ = *src_argv
++) != NULL
)
764 usage(struct bsdtar
*bsdtar
)
768 p
= bsdtar
->progname
;
770 fprintf(stderr
, "Usage:\n");
771 fprintf(stderr
, " List: %s -tf <archive-filename>\n", p
);
772 fprintf(stderr
, " Extract: %s -xf <archive-filename>\n", p
);
773 fprintf(stderr
, " Create: %s -cf <archive-filename> [filenames...]\n", p
);
774 #ifdef HAVE_GETOPT_LONG
775 fprintf(stderr
, " Help: %s --help\n", p
);
777 fprintf(stderr
, " Help: %s -h\n", p
);
785 printf("bsdtar %s - %s\n",
786 BSDTAR_VERSION_STRING
,
791 static const char *long_help_msg
=
792 "First option must be a mode specifier:\n"
793 " -c Create -r Add/Replace -t List -u Update -x Extract\n"
795 " -b # Use # 512-byte records per I/O block\n"
796 " -f <filename> Location of archive (default " _PATH_DEFTAPE
")\n"
799 "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
800 " <file>, <dir> add these items to archive\n"
801 " -z, -j Compress archive with gzip/bzip2\n"
802 " --format {ustar|pax|cpio|shar} Select archive format\n"
803 #ifdef HAVE_GETOPT_LONG
804 " --exclude <pattern> Skip files that match pattern\n"
806 " -W exclude=<pattern> Skip files that match pattern\n"
808 " -C <dir> Change to <dir> before processing remaining files\n"
809 " @<archive> Add entries from <archive> to output\n"
810 "List: %p -t [options] [<patterns>]\n"
811 " <patterns> If specified, list only entries that match\n"
812 "Extract: %p -x [options] [<patterns>]\n"
813 " <patterns> If specified, extract only entries that match\n"
814 " -k Keep (don't overwrite) existing files\n"
815 " -m Don't restore modification times\n"
816 " -O Write entries to stdout, don't restore to disk\n"
817 " -p Restore permissions (including ACLs, owner, file flags)\n";
821 * Note that the word 'bsdtar' will always appear in the first line
824 * In particular, /bin/sh scripts that need to test for the presence
825 * of bsdtar can use the following template:
827 * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
828 * echo bsdtar; else echo not bsdtar; fi
831 long_help(struct bsdtar
*bsdtar
)
836 prog
= bsdtar
->progname
;
840 p
= (strcmp(prog
,"bsdtar") != 0) ? "(bsdtar)" : "";
841 printf("%s%s: manipulate archive files\n", prog
, p
);
843 for (p
= long_help_msg
; *p
!= '\0'; p
++) {
857 bsdtar_getopt(struct bsdtar
*bsdtar
, const char *optstring
,
858 const struct option
**poption
)
861 const struct option
*option
;
864 size_t option_length
;
869 #ifdef HAVE_GETOPT_LONG
870 opt
= getopt_long(bsdtar
->argc
, bsdtar
->argv
, optstring
,
871 tar_longopts
, &option_index
);
872 if (option_index
> -1)
873 *poption
= tar_longopts
+ option_index
;
875 opt
= getopt(bsdtar
->argc
, bsdtar
->argv
, optstring
);
878 /* Support long options through -W longopt=value */
881 q
= strchr(optarg
, '=');
883 option_length
= (size_t)(q
- p
);
886 option_length
= strlen(p
);
889 option
= tar_longopts
;
890 while (option
->name
!= NULL
&&
891 (strlen(option
->name
) < option_length
||
892 strncmp(p
, option
->name
, option_length
) != 0 )) {
896 if (option
->name
!= NULL
) {
900 /* If the first match was exact, we're done. */
901 if (strncmp(p
, option
->name
, strlen(option
->name
)) == 0) {
902 while (option
->name
!= NULL
)
905 /* Check if there's another match. */
907 while (option
->name
!= NULL
&&
908 (strlen(option
->name
) < option_length
||
909 strncmp(p
, option
->name
, option_length
) != 0)) {
913 if (option
->name
!= NULL
)
914 bsdtar_errc(bsdtar
, 1, 0,
915 "Ambiguous option %s "
916 "(matches both %s and %s)",
917 p
, (*poption
)->name
, option
->name
);
919 if ((*poption
)->has_arg
== required_argument
921 bsdtar_errc(bsdtar
, 1, 0,
922 "Option \"%s\" requires argument", p
);
925 /* TODO: Set up a fake 'struct option' for
926 * error reporting... ? ? ? */