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