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