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