diff --git a/usr.bin/tar/bsdtar.1 b/usr.bin/tar/bsdtar.1 index 5a7c32aacd62..db437ed96dbe 100644 --- a/usr.bin/tar/bsdtar.1 +++ b/usr.bin/tar/bsdtar.1 @@ -1,782 +1,786 @@ .\" Copyright (c) 2003-2007 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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. .\" .\" $FreeBSD$ .\" .Dd April 13, 2004 .Dt BSDTAR 1 .Os .Sh NAME .Nm tar .Nd manipulate tape archives .Sh SYNOPSIS .Nm .Op Ar bundled-flags Ao args Ac .Op Ao Ar file Ac | Ao Ar pattern Ac ... .Nm .Brq Fl c .Op Ar options .Op Ar files | directories .Nm .Brq Fl r | Fl u .Fl f Ar archive-file .Op Ar options .Op Ar files | directories .Nm .Brq Fl t | Fl x .Op Ar options .Op Ar patterns .Sh DESCRIPTION .Nm creates and manipulates streaming archive files. This implementation can extract from tar, pax, cpio, zip, jar, ar, and ISO 9660 cdrom images and can create tar, pax, cpio, ar, and shar archives. .Pp The first synopsis form shows a .Dq bundled option word. This usage is provided for compatibility with historical implementations. See COMPATIBILITY below for details. .Pp The other synopsis forms show the preferred usage. The first option to .Nm is a mode indicator from the following list: .Bl -tag -compact -width indent .It Fl c Create a new archive containing the specified items. .It Fl r Like .Fl c , but new entries are appended to the archive. Note that this only works on uncompressed archives stored in regular files. The .Fl f option is required. .It Fl t List archive contents to stdout. .It Fl u Like .Fl r , but new entries are added only if they have a modification date newer than the corresponding entry in the archive. Note that this only works on uncompressed archives stored in regular files. The .Fl f option is required. .It Fl x Extract to disk from the archive. If a file with the same name appears more than once in the archive, each copy will be extracted, with later copies overwriting (replacing) earlier copies. .El .Pp In .Fl c , .Fl r , or .Fl u mode, each specified file or directory is added to the archive in the order specified on the command line. By default, the contents of each directory are also archived. .Pp In extract or list mode, the entire command line is read and parsed before the archive is opened. The pathnames or patterns on the command line indicate which items in the archive should be processed. Patterns are shell-style globbing patterns as documented in .Xr tcsh 1 . .Sh OPTIONS Unless specifically stated otherwise, options are applicable in all operating modes. .Bl -tag -width indent .It Cm @ Ns Pa archive (c and r mode only) The specified archive is opened and the entries in it will be appended to the current archive. As a simple example, .Dl Nm Fl c Fl f Pa - Pa newfile Cm @ Ns Pa original.tar writes a new archive to standard output containing a file .Pa newfile and all of the entries from .Pa original.tar . In contrast, .Dl Nm Fl c Fl f Pa - Pa newfile Pa original.tar creates a new archive with only two entries. Similarly, .Dl Nm Fl czf Pa - Fl -format Cm pax Cm @ Ns Pa - reads an archive from standard input (whose format will be determined automatically) and converts it into a gzip-compressed pax-format archive on stdout. In this way, .Nm can be used to convert archives from one format to another. .It Fl b Ar blocksize Specify the block size, in 512-byte records, for tape drive I/O. As a rule, this argument is only needed when reading from or writing to tape drives, and usually not even then as the default block size of 20 records (10240 bytes) is very common. .It Fl C Ar directory In c and r mode, this changes the directory before adding the following files. In x mode, change directories after opening the archive but before extracting entries from the archive. .It Fl -check-links ( Fl W Cm check-links ) (c and r modes only) Issue a warning message unless all links to each file are archived. .It Fl -chroot ( Fl W Cm chroot ) (x mode only) .Fn chroot to the current directory after processing any .Fl C options and before extracting any files. .It Fl -exclude Ar pattern ( Fl W Cm exclude Ns = Ns Ar pattern ) Do not process files or directories that match the specified pattern. Note that exclusions take precedence over patterns or filenames specified on the command line. .It Fl -format Ar format ( Fl W Cm format Ns = Ns Ar format ) (c, r, u mode only) Use the specified format for the created archive. Supported formats include .Dq cpio , .Dq pax , .Dq shar , and .Dq ustar . Other formats may also be supported; see .Xr libarchive-formats 5 for more information about currently-supported formats. In r and u modes, when extending an existing archive, the format specified here must be compatible with the format of the existing archive on disk. .It Fl f Ar file Read the archive from or write the archive to the specified file. The filename can be .Pa - for standard input or standard output. If not specified, the default tape device will be used. (On .Fx , the default tape device is .Pa /dev/sa0 . ) .It Fl H (c and r mode only) Symbolic links named on the command line will be followed; the target of the link will be archived, not the link itself. .It Fl h (c and r mode only) Synonym for .Fl L . .It Fl I Synonym for .Fl T . .It Fl -include Ar pattern ( Fl W Cm include Ns = Ns Ar pattern ) Process only files or directories that match the specified pattern. Note that exclusions specified with .Fl -exclude take precedence over inclusions. If no inclusions are explicitly specified, all entries are processed by default. The .Fl -include option is especially useful when filtering archives. For example, the command .Dl Nm Fl c Fl f Pa new.tar Fl -include='*foo*' Cm @ Ns Pa old.tgz creates a new archive .Pa new.tar containing only the entries from .Pa old.tgz containing the string .Sq foo . .It Fl j (c mode only) Compress the resulting archive with .Xr bzip2 1 . In extract or list modes, this option is ignored. Note that, unlike other .Nm tar implementations, this implementation recognizes bzip2 compression automatically when reading archives. .It Fl k (x mode only) Do not overwrite existing files. In particular, if a file appears more than once in an archive, later copies will not overwrite earlier copies. +.It Fl -keep-newer-files ( Fl W Cm keep-newer-files ) +(x mode only) +Do not overwrite existing files that are newer than the +versions appearing in the archive being extracted. .It Fl L (c and r mode only) All symbolic links will be followed. Normally, symbolic links are archived as such. With this option, the target of the link will be archived instead. .It Fl l This is a synonym for the .Fl -check-links option. .It Fl m (x mode only) Do not extract modification time. By default, the modification time is set to the time stored in the archive. .It Fl n (c, r, u modes only) Do not recursively archive the contents of directories. .It Fl -newer Ar date ( Fl W Cm newer Ns = Ns Ar date ) (c, r, u modes only) Only include files and directories newer than the specified date. This compares ctime entries. .It Fl -newer-mtime Ar date ( Fl W Cm newer-mtime Ns = Ns Ar date ) (c, r, u modes only) Like .Fl -newer , except it compares mtime entries instead of ctime entries. .It Fl -newer-than Pa file ( Fl W Cm newer-than Ns = Ns Pa file ) (c, r, u modes only) Only include files and directories newer than the specified file. This compares ctime entries. .It Fl -newer-mtime-than Pa file ( Fl W Cm newer-mtime-than Ns = Ns Pa file ) (c, r, u modes only) Like .Fl -newer-than , except it compares mtime entries instead of ctime entries. .It Fl -nodump ( Fl W Cm nodump ) (c and r modes only) Honor the nodump file flag by skipping this file. .It Fl -null ( Fl W Cm null ) (use with .Fl I , .Fl T , or .Fl X ) Filenames or patterns are separated by null characters, not by newlines. This is often used to read filenames output by the .Fl print0 option to .Xr find 1 . .It Fl O (x, t modes only) In extract (-x) mode, files will be written to standard out rather than being extracted to disk. In list (-t) mode, the file listing will be written to stderr rather than the usual stdout. .It Fl o (x mode) Use the user and group of the user running the program rather than those specified in the archive. Note that this has no significance unless .Fl p is specified, and the program is being run by the root user. In this case, the file modes and flags from the archive will be restored, but ACLs or owner information in the archive will be discarded. .Pp (c, r, u mode) A synonym for .Fl -format Ar ustar .It Fl -one-file-system ( Fl W Cm one-file-system ) (c, r, and u modes) Do not cross mount points. .It Fl P Preserve pathnames. By default, absolute pathnames (those that begin with a / character) have the leading slash removed both when creating archives and extracting from them. Also, .Nm will refuse to extract archive entries whose pathnames contain .Pa .. or whose target directory would be altered by a symlink. This option suppresses these behaviors. .It Fl p (x mode only) Preserve file permissions. Attempt to restore the full permissions, including owner, file modes, file flags and ACLs, if available, for each item extracted from the archive. By default, newly-created files are owned by the user running .Nm , the file mode is restored for newly-created regular files, and all other types of entries receive default permissions. If .Nm is being run by root, the default is to restore the owner unless the .Fl o option is also specified. .It Fl q ( Fl -fast-read ) (x and t mode only) Extract or list only the first archive entry that matches each pattern or filename operand. Exit as soon as each specified pattern or filename has been matched. By default, the archive is always read to the very end, since there can be multiple entries with the same name and, by convention, later entries overwrite earlier entries. This option is provided as a performance optimization. .It Fl -strip-components Ar count ( Fl W Cm strip-components Ns = Ns Ar count ) (x and t mode only) Remove the specified number of leading path elements. Pathnames with fewer elements will be silently skipped. Note that the pathname is edited after checking inclusion/exclusion patterns but before security checks. .It Fl T Ar filename In x or t mode, .Nm will read the list of names to be extracted from .Pa filename . In c mode, .Nm will read names to be archived from .Pa filename . The special name .Dq -C on a line by itself will cause the current directory to be changed to the directory specified on the following line. Names are terminated by newlines unless .Fl -null is specified. Note that .Fl -null also disables the special handling of lines containing .Dq -C . .It Fl U (x mode only) Unlink files before creating them. Without this option, .Nm overwrites existing files, which preserves existing hardlinks. With this option, existing hardlinks will be broken, as will any symlink that would affect the location of an extracted file. .It Fl -use-compress-program Ar program Pipe the input (in x or t mode) or the output (in c mode) through .Pa program instead of using the builtin compression support. .It Fl v Produce verbose output. In create and extract modes, .Nm will list each file name as it is read from or written to the archive. In list mode, .Nm will produce output similar to that of .Xr ls 1 . Additional .Fl v options will provide additional detail. .It Fl W Ar longopt=value Long options (preceded by .Fl - ) are only supported directly on systems that have the .Xr getopt_long 3 function. The .Fl W option can be used to access long options on systems that do not support this function. .It Fl w Ask for confirmation for every action. .It Fl X Ar filename Read a list of exclusion patterns from the specified file. See .Fl -exclude for more information about the handling of exclusions. .It Fl y (c mode only) Compress the resulting archive with .Xr bzip2 1 . In extract or list modes, this option is ignored. Note that, unlike other .Nm tar implementations, this implementation recognizes bzip2 compression automatically when reading archives. .It Fl z (c mode only) Compress the resulting archive with .Xr gzip 1 . In extract or list modes, this option is ignored. Note that, unlike other .Nm tar implementations, this implementation recognizes gzip compression automatically when reading archives. .It Fl Z (c mode only) Compress the resulting archive with .Xr compress 1 . In extract or list modes, this option is ignored. Note that, unlike other .Nm tar implementations, this implementation recognizes compress compression automatically when reading archives. .El .Sh ENVIRONMENT The following environment variables affect the execution of .Nm : .Bl -tag -width ".Ev BLOCKSIZE" .It Ev LANG The locale to use. See .Xr environ 7 for more information. .It Ev TAPE The default tape device. The .Fl f option overrides this. .It Ev TZ The timezone to use when displaying dates. See .Xr environ 7 for more information. .El .Sh FILES .Bl -tag -width ".Ev BLOCKSIZE" .It Pa /dev/sa0 The default tape device, if not overridden by the .Ev TAPE environment variable or the .Fl f option. .El .Sh EXIT STATUS .Ex -std .Sh EXAMPLES The following creates a new archive called .Ar file.tar.gz that contains two files .Ar source.c and .Ar source.h : .Dl Nm Fl czf Pa file.tar.gz Pa source.c Pa source.h .Pp To view a detailed table of contents for this archive: .Dl Nm Fl tvf Pa file.tar.gz .Pp To extract all entries from the archive on the default tape drive: .Dl Nm Fl x .Pp To examine the contents of an ISO 9660 cdrom image: .Dl Nm Fl tf Pa image.iso .Pp To move file hierarchies, invoke .Nm as .Dl Nm Fl cf Pa - Fl C Pa srcdir\ . | Nm Fl xpf Pa - Fl C Pa destdir or more traditionally .Dl cd srcdir \&; Nm Fl cf Pa -\ . | ( cd destdir \&; Nm Fl xpf Pa - ) .Pp In create mode, the list of files and directories to be archived can also include directory change instructions of the form .Cm -C Ns Pa foo/baz and archive inclusions of the form .Cm @ Ns Pa archive-file . For example, the command line .Dl Nm Fl c Fl f Pa new.tar Pa foo1 Cm @ Ns Pa old.tgz Cm -C Ns Pa /tmp Pa foo2 will create a new archive .Pa new.tar . .Nm will read the file .Pa foo1 from the current directory and add it to the output archive. It will then read each entry from .Pa old.tgz and add those entries to the output archive. Finally, it will switch to the .Pa /tmp directory and add .Pa foo2 to the output archive. .Pp The .Fl -newer and .Fl -newer-mtime switches accept a variety of common date and time specifications, including .Dq 12 Mar 2005 7:14:29pm , .Dq 2005-03-12 19:14 , .Dq 5 minutes ago , and .Dq 19:14 PST May 1 . .Sh COMPATIBILITY The bundled-arguments format is supported for compatibility with historic implementations. It consists of an initial word (with no leading - character) in which each character indicates an option. Arguments follow as separate words. The order of the arguments must match the order of the corresponding characters in the bundled command word. For example, .Dl Nm Cm tbf 32 Pa file.tar specifies three flags .Cm t , .Cm b , and .Cm f . The .Cm b and .Cm f flags both require arguments, so there must be two additional items on the command line. The .Ar 32 is the argument to the .Cm b flag, and .Ar file.tar is the argument to the .Cm f flag. .Pp The mode options c, r, t, u, and x and the options b, f, l, m, o, v, and w comply with SUSv2. .Pp For maximum portability, scripts that invoke .Nm tar should use the bundled-argument format above, should limit themselves to the .Cm c , .Cm t , and .Cm x modes, and the .Cm b , .Cm f , .Cm m , .Cm v , and .Cm w options. .Pp On systems that support getopt_long(), additional long options are available to improve compatibility with other tar implementations. .Sh SECURITY Certain security issues are common to many archiving programs, including .Nm . In particular, carefully-crafted archives can request that .Nm extract files to locations outside of the target directory. This can potentially be used to cause unwitting users to overwrite files they did not intend to overwrite. If the archive is being extracted by the superuser, any file on the system can potentially be overwritten. There are three ways this can happen. Although .Nm has mechanisms to protect against each one, savvy users should be aware of the implications: .Bl -bullet -width indent .It Archive entries can have absolute pathnames. By default, .Nm removes the leading .Pa / character from filenames before restoring them to guard against this problem. .It Archive entries can have pathnames that include .Pa .. components. By default, .Nm will not extract files containing .Pa .. components in their pathname. .It Archive entries can exploit symbolic links to restore files to other directories. An archive can restore a symbolic link to another directory, then use that link to restore a file into that directory. To guard against this, .Nm checks each extracted path for symlinks. If the final path element is a symlink, it will be removed and replaced with the archive entry. If .Fl U is specified, any intermediate symlink will also be unconditionally removed. If neither .Fl U nor .Fl P is specified, .Nm will refuse to extract the entry. .El To protect yourself, you should be wary of any archives that come from untrusted sources. You should examine the contents of an archive with .Dl Nm Fl tf Pa filename before extraction. You should use the .Fl k option to ensure that .Nm will not overwrite any existing files or the .Fl U option to remove any pre-existing files. You should generally not extract archives while running with super-user privileges. Note that the .Fl P option to .Nm disables the security checks above and allows you to extract an archive while preserving any absolute pathnames, .Pa .. components, or symlinks to other directories. .Sh SEE ALSO .Xr bzip2 1 , .Xr compress 1 , .Xr cpio 1 , .Xr gzip 1 , .Xr mt 1 , .Xr pax 1 , .Xr shar 1 , .Xr libarchive 3 , .Xr libarchive-formats 5 , .Xr tar 5 .Sh STANDARDS There is no current POSIX standard for the tar command; it appeared in .St -p1003.1-96 but was dropped from .St -p1003.1-2001 . The options used by this implementation were developed by surveying a number of existing tar implementations as well as the old POSIX specification for tar and the current POSIX specification for pax. .Pp The ustar and pax interchange file formats are defined by .St -p1003.1-2001 for the pax command. .Sh HISTORY A .Nm tar command appeared in Seventh Edition Unix, which was released in January, 1979. There have been numerous other implementations, many of which extended the file format. John Gilmore's .Nm pdtar public-domain implementation (circa November, 1987) was quite influential, and formed the basis of GNU tar. GNU tar was included as the standard system tar in .Fx beginning with .Fx 1.0 . .Pp This is a complete re-implementation based on the .Xr libarchive 3 library. .Sh BUGS This program follows .St -p1003.1-96 for the definition of the .Fl l option. Note that GNU tar prior to version 1.15 treated .Fl l as a synonym for the .Fl -one-file-system option. .Pp The .Fl C Pa dir option may differ from historic implementations. .Pp All archive output is written in correctly-sized blocks, even if the output is being compressed. Whether or not the last output block is padded to a full block size varies depending on the format and the output device. For tar and cpio formats, the last block of output is padded to a full block size if the output is being written to standard output or to a character or block device such as a tape drive. If the output is being written to a regular file, the last block will not be padded. Many compressors, including .Xr gzip 1 and .Xr bzip2 1 , complain about the null padding when decompressing an archive created by .Nm , although they still extract it correctly. .Pp The compression and decompression is implemented internally, so there may be insignificant differences between the compressed output generated by .Dl Nm Fl czf Pa - file and that generated by .Dl Nm Fl cf Pa - file | Nm gzip .Pp The default should be to read and write archives to the standard I/O paths, but tradition (and POSIX) dictates otherwise. .Pp The .Cm r and .Cm u modes require that the archive be uncompressed and located in a regular file on disk. Other archives can be modified using .Cm c mode with the .Pa @archive-file extension. .Pp To archive a file called .Pa @foo or .Pa -foo you must specify it as .Pa ./@foo or .Pa ./-foo , respectively. .Pp In create mode, a leading .Pa ./ is always removed. A leading .Pa / is stripped unless the .Fl P option is specified. .Pp There needs to be better support for file selection on both create and extract. .Pp There is not yet any support for multi-volume archives or for archiving sparse files. .Pp Converting between dissimilar archive formats (such as tar and cpio) using the .Cm @ Ns Pa - convention can cause hard link information to be lost. (This is a consequence of the incompatible ways that different archive formats store hardlink information.) .Pp There are alternative long options for many of the short options that are deliberately not documented. diff --git a/usr.bin/tar/bsdtar.c b/usr.bin/tar/bsdtar.c index 7e33e5bf3dcd..f86da2fe7bb8 100644 --- a/usr.bin/tar/bsdtar.c +++ b/usr.bin/tar/bsdtar.c @@ -1,934 +1,939 @@ /*- * Copyright (c) 2003-2007 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 #endif #ifdef HAVE_SYS_STAT_H #include #endif #ifdef HAVE_ERRNO_H #include #endif #ifdef HAVE_FCNTL_H #include #endif #ifdef HAVE_GETOPT_LONG #include #else struct option { const char *name; int has_arg; int *flag; int val; }; #define no_argument 0 #define required_argument 1 #endif #ifdef HAVE_LANGINFO_H #include #endif #ifdef HAVE_LOCALE_H #include #endif #ifdef HAVE_PATHS_H #include #endif #include #ifdef HAVE_STDLIB_H #include #endif #ifdef HAVE_STRING_H #include #endif #ifdef HAVE_TIME_H #include #endif #ifdef HAVE_UNISTD_H #include #endif #if HAVE_ZLIB_H #include #endif #include "bsdtar.h" #if !HAVE_DECL_OPTARG extern int optarg; #endif #if !HAVE_DECL_OPTIND extern int optind; #endif /* * 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 #ifndef _PATH_DEFTAPE #define _PATH_DEFTAPE "/dev/tape" #endif /* External function to parse a date/time string (from getdate.y) */ time_t get_date(const char *); static int bsdtar_getopt(struct bsdtar *, const char *optstring, const struct option **poption); static void long_help(struct bsdtar *); static void only_mode(struct bsdtar *, const char *opt, const char *valid); static char ** rewrite_argv(struct bsdtar *, int *argc, char ** src_argv, const char *optstring); static void set_mode(struct bsdtar *, char opt); static void version(void); /* * The leading '+' here forces the GNU version of getopt() (as well as * both the GNU and BSD versions of getopt_long) to stop at the first * non-option. Otherwise, GNU getopt() permutes the arguments and * screws up -C processing. */ static const char *tar_opts = "+Bb:C:cf:HhI:jkLlmnOoPprtT:UuvW:wX:xyZz"; /* * Most of these long options are deliberately not documented. They * are provided only to make life easier for people who also use GNU tar. * The only long options documented in the manual page are the ones * with no corresponding short option, such as --exclude, --nodump, * and --fast-read. * * On systems that lack getopt_long, long options can be specified * using -W longopt and -W longopt=value, e.g. "-W nodump" is the same * as "--nodump" and "-W exclude=pattern" is the same as "--exclude * pattern". This does not rely the GNU getopt() "W;" extension, so * should work correctly on any system with a POSIX-compliant getopt(). */ /* Fake short equivalents for long options that otherwise lack them. */ enum { OPTION_CHECK_LINKS = 1, OPTION_CHROOT, OPTION_EXCLUDE, OPTION_FORMAT, OPTION_HELP, OPTION_INCLUDE, + OPTION_KEEP_NEWER_FILES, OPTION_NEWER_CTIME, OPTION_NEWER_CTIME_THAN, OPTION_NEWER_MTIME, OPTION_NEWER_MTIME_THAN, OPTION_NODUMP, OPTION_NO_SAME_OWNER, OPTION_NO_SAME_PERMISSIONS, OPTION_NULL, OPTION_ONE_FILE_SYSTEM, OPTION_POSIX, OPTION_STRIP_COMPONENTS, OPTION_TOTALS, OPTION_USE_COMPRESS_PROGRAM, OPTION_VERSION }; /* * If you add anything, be very careful to keep this list properly * sorted, as the -W logic relies on it. */ static const struct option tar_longopts[] = { { "absolute-paths", no_argument, NULL, 'P' }, { "append", no_argument, NULL, 'r' }, { "block-size", required_argument, NULL, 'b' }, { "bunzip2", no_argument, NULL, 'j' }, { "bzip", no_argument, NULL, 'j' }, { "bzip2", no_argument, NULL, 'j' }, { "cd", required_argument, NULL, 'C' }, { "check-links", no_argument, NULL, OPTION_CHECK_LINKS }, { "chroot", no_argument, NULL, OPTION_CHROOT }, { "compress", no_argument, NULL, 'Z' }, { "confirmation", no_argument, NULL, 'w' }, { "create", no_argument, NULL, 'c' }, { "dereference", no_argument, NULL, 'L' }, { "directory", required_argument, NULL, 'C' }, { "exclude", required_argument, NULL, OPTION_EXCLUDE }, { "exclude-from", required_argument, NULL, 'X' }, { "extract", no_argument, NULL, 'x' }, { "fast-read", no_argument, NULL, 'q' }, { "file", required_argument, NULL, 'f' }, { "files-from", required_argument, NULL, 'T' }, { "format", required_argument, NULL, OPTION_FORMAT }, { "gunzip", no_argument, NULL, 'z' }, { "gzip", no_argument, NULL, 'z' }, { "help", no_argument, NULL, OPTION_HELP }, { "include", required_argument, NULL, OPTION_INCLUDE }, { "interactive", no_argument, NULL, 'w' }, { "insecure", no_argument, NULL, 'P' }, + { "keep-newer-files", no_argument, NULL, OPTION_KEEP_NEWER_FILES }, { "keep-old-files", no_argument, NULL, 'k' }, { "list", no_argument, NULL, 't' }, { "modification-time", no_argument, NULL, 'm' }, { "newer", required_argument, NULL, OPTION_NEWER_CTIME }, { "newer-ctime", required_argument, NULL, OPTION_NEWER_CTIME }, { "newer-ctime-than", required_argument, NULL, OPTION_NEWER_CTIME_THAN }, { "newer-mtime", required_argument, NULL, OPTION_NEWER_MTIME }, { "newer-mtime-than", required_argument, NULL, OPTION_NEWER_MTIME_THAN }, { "newer-than", required_argument, NULL, OPTION_NEWER_CTIME_THAN }, { "nodump", no_argument, NULL, OPTION_NODUMP }, { "norecurse", no_argument, NULL, 'n' }, { "no-recursion", no_argument, NULL, 'n' }, { "no-same-owner", no_argument, NULL, OPTION_NO_SAME_OWNER }, { "no-same-permissions",no_argument, NULL, OPTION_NO_SAME_PERMISSIONS }, { "null", no_argument, NULL, OPTION_NULL }, { "one-file-system", no_argument, NULL, OPTION_ONE_FILE_SYSTEM }, { "posix", no_argument, NULL, OPTION_POSIX }, { "preserve-permissions", no_argument, NULL, 'p' }, { "read-full-blocks", no_argument, NULL, 'B' }, { "same-permissions", no_argument, NULL, 'p' }, { "strip-components", required_argument, NULL, OPTION_STRIP_COMPONENTS }, { "to-stdout", no_argument, NULL, 'O' }, { "totals", no_argument, NULL, OPTION_TOTALS }, { "uncompress", no_argument, NULL, 'Z' }, { "unlink", no_argument, NULL, 'U' }, { "unlink-first", no_argument, NULL, 'U' }, { "update", no_argument, NULL, 'u' }, { "use-compress-program", required_argument, NULL, OPTION_USE_COMPRESS_PROGRAM }, { "verbose", no_argument, NULL, 'v' }, { "version", no_argument, NULL, OPTION_VERSION }, { NULL, 0, NULL, 0 } }; /* 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; const struct option *option; int opt, t; char option_o; char possible_help_request; char buff[16]; /* * 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; /* Need bsdtar->progname before calling bsdtar_warnc. */ if (*argv == NULL) bsdtar->progname = "bsdtar"; else { bsdtar->progname = strrchr(*argv, '/'); if (bsdtar->progname != NULL) bsdtar->progname++; else bsdtar->progname = *argv; } 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->user_uid == 0) { /* --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; } /* Rewrite traditional-style tar arguments, if used. */ argv = rewrite_argv(bsdtar, &argc, argv, tar_opts); bsdtar->argv = argv; bsdtar->argc = argc; /* Process all remaining arguments now. */ /* * 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, tar_opts, &option)) != -1) { switch (opt) { case 'B': /* GNU tar */ /* libarchive doesn't need this; just ignore it. */ break; case 'b': /* SUSv2 */ t = atoi(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, 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, optarg)) bsdtar_errc(bsdtar, 1, 0, "Couldn't exclude %s\n", optarg); break; case OPTION_FORMAT: /* GNU tar, others */ bsdtar->create_format = optarg; break; case 'f': /* SUSv2 */ bsdtar->filename = 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 = 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, optarg)) bsdtar_errc(bsdtar, 1, 0, "Failed to add %s to inclusion list", 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, "-j 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 '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(optarg); break; case OPTION_NEWER_CTIME_THAN: { struct stat st; if (stat(optarg, &st) != 0) bsdtar_errc(bsdtar, 1, 0, "Can't open file %s", 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(optarg); break; case OPTION_NEWER_MTIME_THAN: { struct stat st; if (stat(optarg, &st) != 0) bsdtar_errc(bsdtar, 1, 0, "Can't open file %s", 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 '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 OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */ bsdtar->strip_components = atoi(optarg); break; case 'T': /* GNU tar */ bsdtar->names_from_file = 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_getop(), so -W is not available here. */ case 'W': /* Obscure, but useful GNU convention. */ break; #endif case 'w': /* SUSv2 */ bsdtar->option_interactive = 1; break; case 'X': /* GNU tar */ if (exclude_from_file(bsdtar, optarg)) bsdtar_errc(bsdtar, 1, 0, "failed to process exclusions from file %s", 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, "-y 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, "-z compression not supported by this version of bsdtar"); usage(bsdtar); #endif break; case OPTION_USE_COMPRESS_PROGRAM: bsdtar->compress_program = 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"); bsdtar->argc -= optind; bsdtar->argv += optind; 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 (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); } /*- * Convert traditional tar arguments into new-style. * For example, * tar tvfb file.tar 32 --exclude FOO * will be converted to * tar -t -v -f file.tar -b 32 --exclude FOO * * This requires building a new argv array. The initial bundled word * gets expanded into a new string that looks like "-t\0-v\0-f\0-b\0". * The new argv array has pointers into this string intermingled with * pointers to the existing arguments. Arguments are moved to * immediately follow their options. * * The optstring argument here is the same one passed to getopt(3). * It is used to determine which option letters have trailing arguments. */ char ** rewrite_argv(struct bsdtar *bsdtar, int *argc, char **src_argv, const char *optstring) { char **new_argv, **dest_argv; const char *p; char *src, *dest; if (src_argv[0] == NULL || src_argv[1] == NULL || src_argv[1][0] == '-') return (src_argv); *argc += strlen(src_argv[1]) - 1; new_argv = malloc((*argc + 1) * sizeof(new_argv[0])); if (new_argv == NULL) bsdtar_errc(bsdtar, 1, errno, "No Memory"); dest_argv = new_argv; *dest_argv++ = *src_argv++; dest = malloc(strlen(*src_argv) * 3); if (dest == NULL) bsdtar_errc(bsdtar, 1, errno, "No memory"); for (src = *src_argv++; *src != '\0'; src++) { *dest_argv++ = dest; *dest++ = '-'; *dest++ = *src; *dest++ = '\0'; /* If option takes an argument, insert that into the list. */ for (p = optstring; p != NULL && *p != '\0'; p++) { if (*p != *src) continue; if (p[1] != ':') /* No arg required, done. */ break; if (*src_argv == NULL) /* No arg available? Error. */ bsdtar_errc(bsdtar, 1, 0, "Option %c requires an argument", *src); *dest_argv++ = *src_argv++; break; } } /* Copy remaining arguments, including trailing NULL. */ while ((*dest_argv++ = *src_argv++) != NULL) ; return (new_argv); } void usage(struct bsdtar *bsdtar) { const char *p; p = bsdtar->progname; fprintf(stderr, "Usage:\n"); fprintf(stderr, " List: %s -tf \n", p); fprintf(stderr, " Extract: %s -xf \n", p); fprintf(stderr, " Create: %s -cf [filenames...]\n", p); #ifdef HAVE_GETOPT_LONG fprintf(stderr, " Help: %s --help\n", p); #else fprintf(stderr, " Help: %s -h\n", p); #endif 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 Location of archive (default " _PATH_DEFTAPE ")\n" " -v Verbose\n" " -w Interactive\n" "Create: %p -c [options] [ | | @ | -C ]\n" " , add these items to archive\n" " -z, -j Compress archive with gzip/bzip2\n" " --format {ustar|pax|cpio|shar} Select archive format\n" #ifdef HAVE_GETOPT_LONG " --exclude Skip files that match pattern\n" #else " -W exclude= Skip files that match pattern\n" #endif " -C Change to before processing remaining files\n" " @ Add entries from to output\n" "List: %p -t [options] []\n" " If specified, list only entries that match\n" "Extract: %p -x [options] []\n" " 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(); } static int bsdtar_getopt(struct bsdtar *bsdtar, const char *optstring, const struct option **poption) { char *p, *q; const struct option *option; int opt; int option_index; size_t option_length; option_index = -1; *poption = NULL; #ifdef HAVE_GETOPT_LONG opt = getopt_long(bsdtar->argc, bsdtar->argv, optstring, tar_longopts, &option_index); if (option_index > -1) *poption = tar_longopts + option_index; #else opt = getopt(bsdtar->argc, bsdtar->argv, optstring); #endif /* Support long options through -W longopt=value */ if (opt == 'W') { p = optarg; q = strchr(optarg, '='); if (q != NULL) { option_length = (size_t)(q - p); optarg = q + 1; } else { option_length = strlen(p); optarg = NULL; } option = tar_longopts; while (option->name != NULL && (strlen(option->name) < option_length || strncmp(p, option->name, option_length) != 0 )) { option++; } if (option->name != NULL) { *poption = option; opt = option->val; /* If the first match was exact, we're done. */ if (strncmp(p, option->name, strlen(option->name)) == 0) { while (option->name != NULL) option++; } else { /* Check if there's another match. */ option++; while (option->name != NULL && (strlen(option->name) < option_length || strncmp(p, option->name, option_length) != 0)) { option++; } } if (option->name != NULL) bsdtar_errc(bsdtar, 1, 0, "Ambiguous option %s " "(matches both %s and %s)", p, (*poption)->name, option->name); if ((*poption)->has_arg == required_argument && optarg == NULL) bsdtar_errc(bsdtar, 1, 0, "Option \"%s\" requires argument", p); } else { opt = '?'; /* TODO: Set up a fake 'struct option' for * error reporting... ? ? ? */ } } return (opt); }