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