2011-12-20 22:47:56 +00:00
|
|
|
/*-
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Command line parser for tar.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bsdtar_platform.h"
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "bsdtar.h"
|
|
|
|
#include "err.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Short options for tar. Please keep this sorted.
|
|
|
|
*/
|
|
|
|
static const char *short_options
|
2013-03-21 18:59:02 +00:00
|
|
|
= "aBb:C:cf:HhI:JjkLlmnOoPpqrSs:T:tUuvW:wX:xyZz";
|
2011-12-20 22:47:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Long options for tar. Please keep this list sorted.
|
|
|
|
*
|
|
|
|
* The symbolic names for options that lack a short equivalent are
|
|
|
|
* defined in bsdtar.h. Also note that so far I've found no need
|
|
|
|
* to support optional arguments to long options. That would be
|
|
|
|
* a small change to the code below.
|
|
|
|
*/
|
|
|
|
|
2012-02-08 12:53:14 +00:00
|
|
|
static const struct bsdtar_option {
|
2011-12-20 22:47:56 +00:00
|
|
|
const char *name;
|
|
|
|
int required; /* 1 if this option requires an argument. */
|
|
|
|
int equivalent; /* Equivalent short option. */
|
|
|
|
} tar_longopts[] = {
|
|
|
|
{ "absolute-paths", 0, 'P' },
|
|
|
|
{ "append", 0, 'r' },
|
2017-03-02 21:02:17 +00:00
|
|
|
{ "acls", 0, OPTION_ACLS },
|
2013-03-21 18:59:02 +00:00
|
|
|
{ "auto-compress", 0, 'a' },
|
|
|
|
{ "b64encode", 0, OPTION_B64ENCODE },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "block-size", 1, 'b' },
|
2016-08-13 21:20:06 +00:00
|
|
|
{ "blocking-factor", 1, 'b' },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "bunzip2", 0, 'j' },
|
|
|
|
{ "bzip", 0, 'j' },
|
|
|
|
{ "bzip2", 0, 'j' },
|
|
|
|
{ "cd", 1, 'C' },
|
|
|
|
{ "check-links", 0, OPTION_CHECK_LINKS },
|
|
|
|
{ "chroot", 0, OPTION_CHROOT },
|
2016-05-11 10:19:44 +00:00
|
|
|
{ "clear-nochange-fflags", 0, OPTION_CLEAR_NOCHANGE_FFLAGS },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "compress", 0, 'Z' },
|
|
|
|
{ "confirmation", 0, 'w' },
|
|
|
|
{ "create", 0, 'c' },
|
|
|
|
{ "dereference", 0, 'L' },
|
|
|
|
{ "directory", 1, 'C' },
|
2017-03-02 21:02:17 +00:00
|
|
|
{ "disable-copyfile", 0, OPTION_NO_MAC_METADATA },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "exclude", 1, OPTION_EXCLUDE },
|
|
|
|
{ "exclude-from", 1, 'X' },
|
Update vendor/libarchive/dist to git b5818e39e128eca4951e2ab10467d4d850a2ba57
Relevant vendor changes:
Issue #795: XAR - do not try to add xattrs without an allocated name
PR #812: non-recursive option for extract and list
PR #958: support reading metadata from compressed files
PR #999: add --exclude-vcs option to bsdtar
Issue #1062: treat empty archives with a GNU volume header as valid
PR #1074: Handle ZIP files with trailing 0s in the extra fields
(Android APK archives)
PR #1109: Ignore padding in Zip extra field data (Android APK archives)
PR #1167: fix problems related to unreadable directories
Issue #1168: fix handling of strtol() and strtoul()
PR #1172: RAR5 - fix invalid window buffer read in E8E9 filter
PR #1174: ZIP reader - fix of MSZIP signature parsing
PR #1175: gzip filter - fix reading files larger than 4GB from memory
PR #1177: gzip filter - fix memory leak with repeated header reads
PR #1180: ZIP reader - add support for Info-ZIP Unicode Path Extra Field
PR #1181: RAR5 - fix merge_block() recursion
(OSS-Fuzz 12999, 13029, 13144, 13478, 13490)
PR #1183: fix memory leak when decompressing ZIP files with LZMA
PR #1184: fix RAR5 OSS-Fuzz issues 12466, 14490, 14491, 12817
OSS-Fuzz 12466: RAR5 - fix buffer overflow when parsing huffman tables
OSS-Fuzz 14490, 14491: RAR5 - fix bad shift-left operations
OSS-Fuzz 12817: RAR5 - handle a case with truncated huffman tables
PR #1186: RAR5 - fix invalid type used for dictionary size mask
(OSS-Fuzz 14537)
PR #1187: RAR5 - fix integer overflow (OSS-Fuzz 14555)
PR #1190: RAR5 - RAR5 don't try to unpack entries marked as directories
(OSS-Fuzz 14574)
PR #1196: RAR5 - fix a potential SIGSEGV on 32-bit builds
OSS-Fuzz 2582: RAR - fix use after free if there is an invalid entry
OSS-Fuzz 14331: RAR5 - fix maximum owner name length
OSS-Fuzz 13965: RAR5 - use unsigned int for volume number + range check
Additional RAR5 reader changes:
- support symlinks, hardlinks, file owner, file group, versioned files
- change ARCHIVE_FORMAT_RAR_V5 to 0x100000
- set correct mode for readonly directories
- support readonly, hidden and system Windows file attributes
NOTE: a version bump of libarchive will happen in the following days
2019-05-20 12:32:00 +00:00
|
|
|
{ "exclude-vcs", 0, OPTION_EXCLUDE_VCS },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "extract", 0, 'x' },
|
|
|
|
{ "fast-read", 0, 'q' },
|
2017-03-02 21:02:17 +00:00
|
|
|
{ "fflags", 0, OPTION_FFLAGS },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "file", 1, 'f' },
|
|
|
|
{ "files-from", 1, 'T' },
|
|
|
|
{ "format", 1, OPTION_FORMAT },
|
|
|
|
{ "gid", 1, OPTION_GID },
|
|
|
|
{ "gname", 1, OPTION_GNAME },
|
2013-03-21 18:59:02 +00:00
|
|
|
{ "grzip", 0, OPTION_GRZIP },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "gunzip", 0, 'z' },
|
|
|
|
{ "gzip", 0, 'z' },
|
|
|
|
{ "help", 0, OPTION_HELP },
|
2013-03-21 18:59:02 +00:00
|
|
|
{ "hfsCompression", 0, OPTION_HFS_COMPRESSION },
|
2016-05-11 10:19:44 +00:00
|
|
|
{ "ignore-zeros", 0, OPTION_IGNORE_ZEROS },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "include", 1, OPTION_INCLUDE },
|
|
|
|
{ "insecure", 0, 'P' },
|
2012-02-08 12:53:14 +00:00
|
|
|
{ "interactive", 0, 'w' },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "keep-newer-files", 0, OPTION_KEEP_NEWER_FILES },
|
|
|
|
{ "keep-old-files", 0, 'k' },
|
|
|
|
{ "list", 0, 't' },
|
2013-03-21 18:59:02 +00:00
|
|
|
{ "lrzip", 0, OPTION_LRZIP },
|
2016-05-11 10:19:44 +00:00
|
|
|
{ "lz4", 0, OPTION_LZ4 },
|
2012-02-08 12:53:14 +00:00
|
|
|
{ "lzip", 0, OPTION_LZIP },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "lzma", 0, OPTION_LZMA },
|
2013-03-21 18:59:02 +00:00
|
|
|
{ "lzop", 0, OPTION_LZOP },
|
2017-03-02 21:02:17 +00:00
|
|
|
{ "mac-metadata", 0, OPTION_MAC_METADATA },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "modification-time", 0, 'm' },
|
|
|
|
{ "newer", 1, OPTION_NEWER_CTIME },
|
|
|
|
{ "newer-ctime", 1, OPTION_NEWER_CTIME },
|
|
|
|
{ "newer-ctime-than", 1, OPTION_NEWER_CTIME_THAN },
|
|
|
|
{ "newer-mtime", 1, OPTION_NEWER_MTIME },
|
|
|
|
{ "newer-mtime-than", 1, OPTION_NEWER_MTIME_THAN },
|
|
|
|
{ "newer-than", 1, OPTION_NEWER_CTIME_THAN },
|
2017-03-02 21:02:17 +00:00
|
|
|
{ "no-acls", 0, OPTION_NO_ACLS },
|
|
|
|
{ "no-fflags", 0, OPTION_NO_FFLAGS },
|
|
|
|
{ "no-mac-metadata", 0, OPTION_NO_MAC_METADATA },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "no-recursion", 0, 'n' },
|
2020-02-11 23:48:03 +00:00
|
|
|
{ "no-safe-writes", 0, OPTION_NO_SAFE_WRITES },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "no-same-owner", 0, OPTION_NO_SAME_OWNER },
|
|
|
|
{ "no-same-permissions", 0, OPTION_NO_SAME_PERMISSIONS },
|
2017-03-02 21:02:17 +00:00
|
|
|
{ "no-xattr", 0, OPTION_NO_XATTRS },
|
|
|
|
{ "no-xattrs", 0, OPTION_NO_XATTRS },
|
2012-02-08 12:53:14 +00:00
|
|
|
{ "nodump", 0, OPTION_NODUMP },
|
2013-03-21 18:59:02 +00:00
|
|
|
{ "nopreserveHFSCompression",0, OPTION_NOPRESERVE_HFS_COMPRESSION },
|
2012-02-08 12:53:14 +00:00
|
|
|
{ "norecurse", 0, 'n' },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "null", 0, OPTION_NULL },
|
|
|
|
{ "numeric-owner", 0, OPTION_NUMERIC_OWNER },
|
2013-03-21 18:59:02 +00:00
|
|
|
{ "older", 1, OPTION_OLDER_CTIME },
|
|
|
|
{ "older-ctime", 1, OPTION_OLDER_CTIME },
|
|
|
|
{ "older-ctime-than", 1, OPTION_OLDER_CTIME_THAN },
|
|
|
|
{ "older-mtime", 1, OPTION_OLDER_MTIME },
|
|
|
|
{ "older-mtime-than", 1, OPTION_OLDER_MTIME_THAN },
|
|
|
|
{ "older-than", 1, OPTION_OLDER_CTIME_THAN },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "one-file-system", 0, OPTION_ONE_FILE_SYSTEM },
|
|
|
|
{ "options", 1, OPTION_OPTIONS },
|
2016-05-11 10:19:44 +00:00
|
|
|
{ "passphrase", 1, OPTION_PASSPHRASE },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "posix", 0, OPTION_POSIX },
|
|
|
|
{ "preserve-permissions", 0, 'p' },
|
|
|
|
{ "read-full-blocks", 0, 'B' },
|
2020-02-11 23:48:03 +00:00
|
|
|
{ "safe-writes", 0, OPTION_SAFE_WRITES },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "same-owner", 0, OPTION_SAME_OWNER },
|
|
|
|
{ "same-permissions", 0, 'p' },
|
|
|
|
{ "strip-components", 1, OPTION_STRIP_COMPONENTS },
|
|
|
|
{ "to-stdout", 0, 'O' },
|
|
|
|
{ "totals", 0, OPTION_TOTALS },
|
|
|
|
{ "uid", 1, OPTION_UID },
|
|
|
|
{ "uname", 1, OPTION_UNAME },
|
|
|
|
{ "uncompress", 0, 'Z' },
|
|
|
|
{ "unlink", 0, 'U' },
|
|
|
|
{ "unlink-first", 0, 'U' },
|
|
|
|
{ "update", 0, 'u' },
|
|
|
|
{ "use-compress-program", 1, OPTION_USE_COMPRESS_PROGRAM },
|
2013-03-21 18:59:02 +00:00
|
|
|
{ "uuencode", 0, OPTION_UUENCODE },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "verbose", 0, 'v' },
|
|
|
|
{ "version", 0, OPTION_VERSION },
|
2017-03-02 21:02:17 +00:00
|
|
|
{ "xattrs", 0, OPTION_XATTRS },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ "xz", 0, 'J' },
|
2017-09-30 23:33:19 +00:00
|
|
|
{ "zstd", 0, OPTION_ZSTD },
|
2011-12-20 22:47:56 +00:00
|
|
|
{ NULL, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This getopt implementation has two key features that common
|
|
|
|
* getopt_long() implementations lack. Apart from those, it's a
|
|
|
|
* straightforward option parser, considerably simplified by not
|
|
|
|
* needing to support the wealth of exotic getopt_long() features. It
|
|
|
|
* has, of course, been shamelessly tailored for bsdtar. (If you're
|
|
|
|
* looking for a generic getopt_long() implementation for your
|
|
|
|
* project, I recommend Gregory Pietsch's public domain getopt_long()
|
|
|
|
* implementation.) The two additional features are:
|
|
|
|
*
|
|
|
|
* Old-style tar arguments: The original tar implementation treated
|
|
|
|
* the first argument word as a list of single-character option
|
|
|
|
* letters. All arguments follow as separate words. For example,
|
|
|
|
* tar xbf 32 /dev/tape
|
|
|
|
* Here, the "xbf" is three option letters, "32" is the argument for
|
|
|
|
* "b" and "/dev/tape" is the argument for "f". We support this usage
|
|
|
|
* if the first command-line argument does not begin with '-'. We
|
|
|
|
* also allow regular short and long options to follow, e.g.,
|
|
|
|
* tar xbf 32 /dev/tape -P --format=pax
|
|
|
|
*
|
|
|
|
* -W long options: There's an obscure GNU convention (only rarely
|
|
|
|
* supported even there) that allows "-W option=argument" as an
|
|
|
|
* alternative way to support long options. This was supported in
|
|
|
|
* early bsdtar as a way to access long options on platforms that did
|
|
|
|
* not support getopt_long() and is preserved here for backwards
|
|
|
|
* compatibility. (Of course, if I'd started with a custom
|
|
|
|
* command-line parser from the beginning, I would have had normal
|
|
|
|
* long option support on every platform so that hack wouldn't have
|
|
|
|
* been necessary. Oh, well. Some mistakes you just have to live
|
|
|
|
* with.)
|
|
|
|
*
|
|
|
|
* TODO: We should be able to use this to pull files and intermingled
|
|
|
|
* options (such as -C) from the command line in write mode. That
|
|
|
|
* will require a little rethinking of the argument handling in
|
|
|
|
* bsdtar.c.
|
|
|
|
*
|
|
|
|
* TODO: If we want to support arbitrary command-line options from -T
|
|
|
|
* input (as GNU tar does), we may need to extend this to handle option
|
2012-02-08 12:53:14 +00:00
|
|
|
* words from sources other than argv/argc. I'm not really sure if I
|
2011-12-20 22:47:56 +00:00
|
|
|
* like that feature of GNU tar, so it's certainly not a priority.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
bsdtar_getopt(struct bsdtar *bsdtar)
|
|
|
|
{
|
|
|
|
enum { state_start = 0, state_old_tar, state_next_word,
|
|
|
|
state_short, state_long };
|
|
|
|
|
2012-02-08 12:53:14 +00:00
|
|
|
const struct bsdtar_option *popt, *match = NULL, *match2 = NULL;
|
2011-12-20 22:47:56 +00:00
|
|
|
const char *p, *long_prefix = "--";
|
|
|
|
size_t optlength;
|
|
|
|
int opt = '?';
|
|
|
|
int required = 0;
|
|
|
|
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->argument = NULL;
|
2011-12-20 22:47:56 +00:00
|
|
|
|
|
|
|
/* First time through, initialize everything. */
|
2012-02-08 12:53:14 +00:00
|
|
|
if (bsdtar->getopt_state == state_start) {
|
2011-12-20 22:47:56 +00:00
|
|
|
/* Skip program name. */
|
|
|
|
++bsdtar->argv;
|
|
|
|
--bsdtar->argc;
|
|
|
|
if (*bsdtar->argv == NULL)
|
|
|
|
return (-1);
|
|
|
|
/* Decide between "new style" and "old style" arguments. */
|
|
|
|
if (bsdtar->argv[0][0] == '-') {
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->getopt_state = state_next_word;
|
2011-12-20 22:47:56 +00:00
|
|
|
} else {
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->getopt_state = state_old_tar;
|
|
|
|
bsdtar->getopt_word = *bsdtar->argv++;
|
2011-12-20 22:47:56 +00:00
|
|
|
--bsdtar->argc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're parsing old-style tar arguments
|
|
|
|
*/
|
2012-02-08 12:53:14 +00:00
|
|
|
if (bsdtar->getopt_state == state_old_tar) {
|
2011-12-20 22:47:56 +00:00
|
|
|
/* Get the next option character. */
|
2012-02-08 12:53:14 +00:00
|
|
|
opt = *bsdtar->getopt_word++;
|
2011-12-20 22:47:56 +00:00
|
|
|
if (opt == '\0') {
|
|
|
|
/* New-style args can follow old-style. */
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->getopt_state = state_next_word;
|
2011-12-20 22:47:56 +00:00
|
|
|
} else {
|
|
|
|
/* See if it takes an argument. */
|
|
|
|
p = strchr(short_options, opt);
|
|
|
|
if (p == NULL)
|
|
|
|
return ('?');
|
|
|
|
if (p[1] == ':') {
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->argument = *bsdtar->argv;
|
|
|
|
if (bsdtar->argument == NULL) {
|
2011-12-20 22:47:56 +00:00
|
|
|
lafe_warnc(0,
|
|
|
|
"Option %c requires an argument",
|
|
|
|
opt);
|
|
|
|
return ('?');
|
|
|
|
}
|
|
|
|
++bsdtar->argv;
|
|
|
|
--bsdtar->argc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're ready to look at the next word in argv.
|
|
|
|
*/
|
2012-02-08 12:53:14 +00:00
|
|
|
if (bsdtar->getopt_state == state_next_word) {
|
2011-12-20 22:47:56 +00:00
|
|
|
/* No more arguments, so no more options. */
|
|
|
|
if (bsdtar->argv[0] == NULL)
|
|
|
|
return (-1);
|
|
|
|
/* Doesn't start with '-', so no more options. */
|
|
|
|
if (bsdtar->argv[0][0] != '-')
|
|
|
|
return (-1);
|
|
|
|
/* "--" marks end of options; consume it and return. */
|
|
|
|
if (strcmp(bsdtar->argv[0], "--") == 0) {
|
|
|
|
++bsdtar->argv;
|
|
|
|
--bsdtar->argc;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/* Get next word for parsing. */
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->getopt_word = *bsdtar->argv++;
|
2011-12-20 22:47:56 +00:00
|
|
|
--bsdtar->argc;
|
2012-02-08 12:53:14 +00:00
|
|
|
if (bsdtar->getopt_word[1] == '-') {
|
2011-12-20 22:47:56 +00:00
|
|
|
/* Set up long option parser. */
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->getopt_state = state_long;
|
|
|
|
bsdtar->getopt_word += 2; /* Skip leading '--' */
|
2011-12-20 22:47:56 +00:00
|
|
|
} else {
|
|
|
|
/* Set up short option parser. */
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->getopt_state = state_short;
|
|
|
|
++bsdtar->getopt_word; /* Skip leading '-' */
|
2011-12-20 22:47:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're parsing a group of POSIX-style single-character options.
|
|
|
|
*/
|
2012-02-08 12:53:14 +00:00
|
|
|
if (bsdtar->getopt_state == state_short) {
|
2011-12-20 22:47:56 +00:00
|
|
|
/* Peel next option off of a group of short options. */
|
2012-02-08 12:53:14 +00:00
|
|
|
opt = *bsdtar->getopt_word++;
|
2011-12-20 22:47:56 +00:00
|
|
|
if (opt == '\0') {
|
|
|
|
/* End of this group; recurse to get next option. */
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->getopt_state = state_next_word;
|
2011-12-20 22:47:56 +00:00
|
|
|
return bsdtar_getopt(bsdtar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does this option take an argument? */
|
|
|
|
p = strchr(short_options, opt);
|
|
|
|
if (p == NULL)
|
|
|
|
return ('?');
|
|
|
|
if (p[1] == ':')
|
|
|
|
required = 1;
|
|
|
|
|
|
|
|
/* If it takes an argument, parse that. */
|
|
|
|
if (required) {
|
2012-02-08 12:53:14 +00:00
|
|
|
/* If arg is run-in, bsdtar->getopt_word already points to it. */
|
|
|
|
if (bsdtar->getopt_word[0] == '\0') {
|
2011-12-20 22:47:56 +00:00
|
|
|
/* Otherwise, pick up the next word. */
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->getopt_word = *bsdtar->argv;
|
|
|
|
if (bsdtar->getopt_word == NULL) {
|
2011-12-20 22:47:56 +00:00
|
|
|
lafe_warnc(0,
|
|
|
|
"Option -%c requires an argument",
|
|
|
|
opt);
|
|
|
|
return ('?');
|
|
|
|
}
|
|
|
|
++bsdtar->argv;
|
|
|
|
--bsdtar->argc;
|
|
|
|
}
|
|
|
|
if (opt == 'W') {
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->getopt_state = state_long;
|
2011-12-20 22:47:56 +00:00
|
|
|
long_prefix = "-W "; /* For clearer errors. */
|
|
|
|
} else {
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->getopt_state = state_next_word;
|
|
|
|
bsdtar->argument = bsdtar->getopt_word;
|
2011-12-20 22:47:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We're reading a long option, including -W long=arg convention. */
|
2012-02-08 12:53:14 +00:00
|
|
|
if (bsdtar->getopt_state == state_long) {
|
2011-12-20 22:47:56 +00:00
|
|
|
/* After this long option, we'll be starting a new word. */
|
2012-02-08 12:53:14 +00:00
|
|
|
bsdtar->getopt_state = state_next_word;
|
2011-12-20 22:47:56 +00:00
|
|
|
|
|
|
|
/* Option name ends at '=' if there is one. */
|
2012-02-08 12:53:14 +00:00
|
|
|
p = strchr(bsdtar->getopt_word, '=');
|
2011-12-20 22:47:56 +00:00
|
|
|
if (p != NULL) {
|
2012-02-08 12:53:14 +00:00
|
|
|
optlength = (size_t)(p - bsdtar->getopt_word);
|
|
|
|
bsdtar->argument = (char *)(uintptr_t)(p + 1);
|
2011-12-20 22:47:56 +00:00
|
|
|
} else {
|
2012-02-08 12:53:14 +00:00
|
|
|
optlength = strlen(bsdtar->getopt_word);
|
2011-12-20 22:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search the table for an unambiguous match. */
|
|
|
|
for (popt = tar_longopts; popt->name != NULL; popt++) {
|
|
|
|
/* Short-circuit if first chars don't match. */
|
2012-02-08 12:53:14 +00:00
|
|
|
if (popt->name[0] != bsdtar->getopt_word[0])
|
2011-12-20 22:47:56 +00:00
|
|
|
continue;
|
|
|
|
/* If option is a prefix of name in table, record it.*/
|
2012-02-08 12:53:14 +00:00
|
|
|
if (strncmp(bsdtar->getopt_word, popt->name, optlength) == 0) {
|
2011-12-20 22:47:56 +00:00
|
|
|
match2 = match; /* Record up to two matches. */
|
|
|
|
match = popt;
|
|
|
|
/* If it's an exact match, we're done. */
|
|
|
|
if (strlen(popt->name) == optlength) {
|
|
|
|
match2 = NULL; /* Forget the others. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fail if there wasn't a unique match. */
|
|
|
|
if (match == NULL) {
|
|
|
|
lafe_warnc(0,
|
|
|
|
"Option %s%s is not supported",
|
2012-02-08 12:53:14 +00:00
|
|
|
long_prefix, bsdtar->getopt_word);
|
2011-12-20 22:47:56 +00:00
|
|
|
return ('?');
|
|
|
|
}
|
|
|
|
if (match2 != NULL) {
|
|
|
|
lafe_warnc(0,
|
|
|
|
"Ambiguous option %s%s (matches --%s and --%s)",
|
2012-02-08 12:53:14 +00:00
|
|
|
long_prefix, bsdtar->getopt_word, match->name, match2->name);
|
2011-12-20 22:47:56 +00:00
|
|
|
return ('?');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We've found a unique match; does it need an argument? */
|
|
|
|
if (match->required) {
|
|
|
|
/* Argument required: get next word if necessary. */
|
2012-02-08 12:53:14 +00:00
|
|
|
if (bsdtar->argument == NULL) {
|
|
|
|
bsdtar->argument = *bsdtar->argv;
|
|
|
|
if (bsdtar->argument == NULL) {
|
2011-12-20 22:47:56 +00:00
|
|
|
lafe_warnc(0,
|
|
|
|
"Option %s%s requires an argument",
|
|
|
|
long_prefix, match->name);
|
|
|
|
return ('?');
|
|
|
|
}
|
|
|
|
++bsdtar->argv;
|
|
|
|
--bsdtar->argc;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Argument forbidden: fail if there is one. */
|
2012-02-08 12:53:14 +00:00
|
|
|
if (bsdtar->argument != NULL) {
|
2011-12-20 22:47:56 +00:00
|
|
|
lafe_warnc(0,
|
|
|
|
"Option %s%s does not allow an argument",
|
|
|
|
long_prefix, match->name);
|
|
|
|
return ('?');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (match->equivalent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (opt);
|
|
|
|
}
|