1
0
mirror of https://git.FreeBSD.org/src.git synced 2025-01-23 16:01:42 +00:00

It shouldn't be an error to specify the same mode twice: allow -cc but not -ct.

Thanks to: Christoph Mallon (whose proposed patch was actually
   simpler than what I ended up with)
This commit is contained in:
Tim Kientzle 2004-08-08 05:10:10 +00:00
parent 3387f49acb
commit dd1a40a3c0
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=133308
2 changed files with 38 additions and 50 deletions

View File

@ -76,11 +76,12 @@ struct option {
static int bsdtar_getopt(struct bsdtar *, const char *optstring, static int bsdtar_getopt(struct bsdtar *, const char *optstring,
const struct option **poption); const struct option **poption);
static void long_help(struct bsdtar *); static void long_help(struct bsdtar *);
static void only_mode(struct bsdtar *, char mode, const char *opt, static void only_mode(struct bsdtar *, const char *opt,
const char *valid); const char *valid);
static char ** rewrite_argv(struct bsdtar *, static char ** rewrite_argv(struct bsdtar *,
int *argc, char ** src_argv, int *argc, char ** src_argv,
const char *optstring); const char *optstring);
static void set_mode(struct bsdtar *, char opt);
static void version(void); static void version(void);
/* /*
@ -171,7 +172,6 @@ main(int argc, char **argv)
struct bsdtar *bsdtar, bsdtar_storage; struct bsdtar *bsdtar, bsdtar_storage;
const struct option *option; const struct option *option;
int opt, t; int opt, t;
char mode;
char option_o; char option_o;
char possible_help_request; char possible_help_request;
char buff[16]; char buff[16];
@ -190,7 +190,6 @@ main(int argc, char **argv)
#if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER) #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd'); bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
#endif #endif
mode = '\0';
possible_help_request = 0; possible_help_request = 0;
/* Look up uid of current user for future reference */ /* Look up uid of current user for future reference */
@ -241,11 +240,7 @@ main(int argc, char **argv)
bsdtar->start_dir = optarg; bsdtar->start_dir = optarg;
break; break;
case 'c': /* SUSv2 */ case 'c': /* SUSv2 */
if (mode != '\0') set_mode(bsdtar, opt);
bsdtar_errc(bsdtar, 1, 0,
"Can't specify both -%c and -%c",
opt, mode);
mode = opt;
break; break;
case OPTION_CHECK_LINKS: /* GNU tar */ case OPTION_CHECK_LINKS: /* GNU tar */
bsdtar->option_warn_links = 1; bsdtar->option_warn_links = 1;
@ -365,21 +360,13 @@ main(int argc, char **argv)
bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
break; break;
case 'r': /* SUSv2 */ case 'r': /* SUSv2 */
if (mode != '\0') set_mode(bsdtar, opt);
bsdtar_errc(bsdtar, 1, 0,
"Can't specify both -%c and -%c",
opt, mode);
mode = opt;
break; break;
case 'T': /* GNU tar */ case 'T': /* GNU tar */
bsdtar->names_from_file = optarg; bsdtar->names_from_file = optarg;
break; break;
case 't': /* SUSv2 */ case 't': /* SUSv2 */
if (mode != '\0') set_mode(bsdtar, opt);
bsdtar_errc(bsdtar, 1, 0,
"Can't specify both -%c and -%c",
opt, mode);
mode = opt;
bsdtar->verbose++; bsdtar->verbose++;
break; break;
case OPTION_TOTALS: /* GNU tar */ case OPTION_TOTALS: /* GNU tar */
@ -390,11 +377,7 @@ main(int argc, char **argv)
bsdtar->option_unlink_first = 1; bsdtar->option_unlink_first = 1;
break; break;
case 'u': /* SUSv2 */ case 'u': /* SUSv2 */
if (mode != '\0') set_mode(bsdtar, opt);
bsdtar_errc(bsdtar, 1, 0,
"Can't specify both -%c and -%c",
opt, mode);
mode = opt;
break; break;
case 'v': /* SUSv2 */ case 'v': /* SUSv2 */
bsdtar->verbose++; bsdtar->verbose++;
@ -412,11 +395,7 @@ main(int argc, char **argv)
optarg); optarg);
break; break;
case 'x': /* SUSv2 */ case 'x': /* SUSv2 */
if (mode != '\0') set_mode(bsdtar, opt);
bsdtar_errc(bsdtar, 1, 0,
"Can't specify both -%c and -%c",
opt, mode);
mode = opt;
break; break;
case 'y': /* FreeBSD version of GNU tar */ case 'y': /* FreeBSD version of GNU tar */
#if HAVE_LIBBZ2 #if HAVE_LIBBZ2
@ -458,26 +437,26 @@ main(int argc, char **argv)
/* /*
* Sanity-check options. * Sanity-check options.
*/ */
if ((mode == '\0') && possible_help_request) { if ((bsdtar->mode == '\0') && possible_help_request) {
long_help(bsdtar); long_help(bsdtar);
exit(0); exit(0);
} }
if (mode == '\0') if (bsdtar->mode == '\0')
bsdtar_errc(bsdtar, 1, 0, bsdtar_errc(bsdtar, 1, 0,
"Must specify one of -c, -r, -t, -u, -x"); "Must specify one of -c, -r, -t, -u, -x");
/* Check boolean options only permitted in certain modes. */ /* Check boolean options only permitted in certain modes. */
if (bsdtar->option_absolute_paths) if (bsdtar->option_absolute_paths)
only_mode(bsdtar, mode, "-P", "xcru"); only_mode(bsdtar, "-P", "xcru");
if (bsdtar->option_dont_traverse_mounts) if (bsdtar->option_dont_traverse_mounts)
only_mode(bsdtar, mode, "-X", "cru"); only_mode(bsdtar, "-X", "cru");
if (bsdtar->option_fast_read) if (bsdtar->option_fast_read)
only_mode(bsdtar, mode, "--fast-read", "xt"); only_mode(bsdtar, "--fast-read", "xt");
if (bsdtar->option_honor_nodump) if (bsdtar->option_honor_nodump)
only_mode(bsdtar, mode, "--nodump", "cru"); only_mode(bsdtar, "--nodump", "cru");
if (option_o > 0) { if (option_o > 0) {
switch (mode) { switch (bsdtar->mode) {
case 'c': case 'c':
/* /*
* In GNU tar, -o means "old format." The * In GNU tar, -o means "old format." The
@ -493,39 +472,39 @@ main(int argc, char **argv)
bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
break; break;
default: default:
only_mode(bsdtar, mode, "-o", "xc"); only_mode(bsdtar, "-o", "xc");
break; break;
} }
} }
if (bsdtar->option_no_subdirs) if (bsdtar->option_no_subdirs)
only_mode(bsdtar, mode, "-n", "cru"); only_mode(bsdtar, "-n", "cru");
if (bsdtar->option_stdout) if (bsdtar->option_stdout)
only_mode(bsdtar, mode, "-O", "x"); only_mode(bsdtar, "-O", "x");
if (bsdtar->option_warn_links) if (bsdtar->option_warn_links)
only_mode(bsdtar, mode, "--check-links", "cr"); only_mode(bsdtar, "--check-links", "cr");
/* Check other parameters only permitted in certain modes. */ /* Check other parameters only permitted in certain modes. */
if (bsdtar->create_compression == 'Z' && mode == 'c') { if (bsdtar->create_compression == 'Z' && bsdtar->mode == 'c') {
bsdtar_warnc(bsdtar, 0, ".Z compression not supported"); bsdtar_warnc(bsdtar, 0, ".Z compression not supported");
usage(bsdtar); usage(bsdtar);
} }
if (bsdtar->create_compression != '\0') { if (bsdtar->create_compression != '\0') {
strcpy(buff, "-?"); strcpy(buff, "-?");
buff[1] = bsdtar->create_compression; buff[1] = bsdtar->create_compression;
only_mode(bsdtar, mode, buff, "cxt"); only_mode(bsdtar, buff, "cxt");
} }
if (bsdtar->create_format != NULL) if (bsdtar->create_format != NULL)
only_mode(bsdtar, mode, "-F", "c"); only_mode(bsdtar, "-F", "c");
if (bsdtar->symlink_mode != '\0') { if (bsdtar->symlink_mode != '\0') {
strcpy(buff, "-?"); strcpy(buff, "-?");
buff[1] = bsdtar->symlink_mode; buff[1] = bsdtar->symlink_mode;
only_mode(bsdtar, mode, buff, "cru"); only_mode(bsdtar, buff, "cru");
} }
bsdtar->argc -= optind; bsdtar->argc -= optind;
bsdtar->argv += optind; bsdtar->argv += optind;
switch(mode) { switch(bsdtar->mode) {
case 'c': case 'c':
tar_mode_c(bsdtar); tar_mode_c(bsdtar);
break; break;
@ -547,17 +526,25 @@ main(int argc, char **argv)
return (bsdtar->return_value); 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. * Verify that the mode is correct.
*/ */
static void static void
only_mode(struct bsdtar *bsdtar, char mode, only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
const char *opt, const char *valid_modes)
{ {
if (strchr(valid_modes, mode) == NULL) if (strchr(valid_modes, bsdtar->mode) == NULL)
bsdtar_errc(bsdtar, 1, 0, bsdtar_errc(bsdtar, 1, 0,
"Option %s is not permitted in mode -%c", "Option %s is not permitted in mode -%c",
opt, mode); opt, bsdtar->mode);
} }
@ -663,7 +650,7 @@ static const char *long_help_msg =
" -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n" " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
" -v Verbose\n" " -v Verbose\n"
" -w Interactive\n" " -w Interactive\n"
"Create: %p -c [options] [<file> | <dir> | @<archive> | C=<dir> ]\n" "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
" <file>, <dir> add these items to archive\n" " <file>, <dir> add these items to archive\n"
" -z, -j Compress archive with gzip/bzip2\n" " -z, -j Compress archive with gzip/bzip2\n"
" -F {ustar|pax|cpio|shar} Select archive format\n" " -F {ustar|pax|cpio|shar} Select archive format\n"
@ -672,7 +659,7 @@ static const char *long_help_msg =
#else #else
" -W exclude=<pattern> Skip files that match pattern\n" " -W exclude=<pattern> Skip files that match pattern\n"
#endif #endif
" C=<dir> Change to <dir> before processing remaining files\n" " -C <dir> Change to <dir> before processing remaining files\n"
" @<archive> Add entries from <archive> to output\n" " @<archive> Add entries from <archive> to output\n"
"List: %p -t [options] [<patterns>]\n" "List: %p -t [options] [<patterns>]\n"
" <patterns> If specified, list only entries that match\n" " <patterns> If specified, list only entries that match\n"

View File

@ -51,6 +51,7 @@ struct bsdtar {
int bytes_per_block; /* -b block_size */ int bytes_per_block; /* -b block_size */
int verbose; /* -v */ int verbose; /* -v */
int extract_flags; /* Flags for extract operation */ int extract_flags; /* Flags for extract operation */
char mode; /* Program mode: 'c', 't', 'r', 'u', 'x' */
char symlink_mode; /* H or L, per BSD conventions */ char symlink_mode; /* H or L, per BSD conventions */
char create_compression; /* j, y, or z */ char create_compression; /* j, y, or z */
char option_absolute_paths; /* -P */ char option_absolute_paths; /* -P */