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