18029ab02SPeter Avalos /*-
28029ab02SPeter Avalos * Copyright (c) 2003-2008 Tim Kientzle
38029ab02SPeter Avalos * All rights reserved.
48029ab02SPeter Avalos *
58029ab02SPeter Avalos * Redistribution and use in source and binary forms, with or without
68029ab02SPeter Avalos * modification, are permitted provided that the following conditions
78029ab02SPeter Avalos * are met:
88029ab02SPeter Avalos * 1. Redistributions of source code must retain the above copyright
98029ab02SPeter Avalos * notice, this list of conditions and the following disclaimer.
108029ab02SPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
118029ab02SPeter Avalos * notice, this list of conditions and the following disclaimer in the
128029ab02SPeter Avalos * documentation and/or other materials provided with the distribution.
138029ab02SPeter Avalos *
148029ab02SPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
158029ab02SPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
168029ab02SPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
178029ab02SPeter Avalos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
188029ab02SPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
198029ab02SPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
208029ab02SPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
218029ab02SPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
228029ab02SPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
238029ab02SPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
248029ab02SPeter Avalos */
258029ab02SPeter Avalos
268029ab02SPeter Avalos /*
278029ab02SPeter Avalos * Command line parser for tar.
288029ab02SPeter Avalos */
298029ab02SPeter Avalos
308029ab02SPeter Avalos #include "bsdtar_platform.h"
318029ab02SPeter Avalos __FBSDID("$FreeBSD$");
328029ab02SPeter Avalos
338029ab02SPeter Avalos #ifdef HAVE_ERRNO_H
348029ab02SPeter Avalos #include <errno.h>
358029ab02SPeter Avalos #endif
368029ab02SPeter Avalos #ifdef HAVE_STDLIB_H
378029ab02SPeter Avalos #include <stdlib.h>
388029ab02SPeter Avalos #endif
398029ab02SPeter Avalos #ifdef HAVE_STRING_H
408029ab02SPeter Avalos #include <string.h>
418029ab02SPeter Avalos #endif
428029ab02SPeter Avalos
438029ab02SPeter Avalos #include "bsdtar.h"
449c82a63eSPeter Avalos #include "err.h"
458029ab02SPeter Avalos
468029ab02SPeter Avalos /*
478029ab02SPeter Avalos * Short options for tar. Please keep this sorted.
488029ab02SPeter Avalos */
498029ab02SPeter Avalos static const char *short_options
50d4d8193eSPeter Avalos = "aBb:C:cf:HhI:JjkLlmnOoPpqrSs:T:tUuvW:wX:xyZz";
518029ab02SPeter Avalos
528029ab02SPeter Avalos /*
538029ab02SPeter Avalos * Long options for tar. Please keep this list sorted.
548029ab02SPeter Avalos *
558029ab02SPeter Avalos * The symbolic names for options that lack a short equivalent are
568029ab02SPeter Avalos * defined in bsdtar.h. Also note that so far I've found no need
578029ab02SPeter Avalos * to support optional arguments to long options. That would be
588029ab02SPeter Avalos * a small change to the code below.
598029ab02SPeter Avalos */
608029ab02SPeter Avalos
61c09f92d2SPeter Avalos static const struct bsdtar_option {
628029ab02SPeter Avalos const char *name;
638029ab02SPeter Avalos int required; /* 1 if this option requires an argument. */
648029ab02SPeter Avalos int equivalent; /* Equivalent short option. */
658029ab02SPeter Avalos } tar_longopts[] = {
668029ab02SPeter Avalos { "absolute-paths", 0, 'P' },
678029ab02SPeter Avalos { "append", 0, 'r' },
68e95abc47Szrj { "acls", 0, OPTION_ACLS },
69d4d8193eSPeter Avalos { "auto-compress", 0, 'a' },
70d4d8193eSPeter Avalos { "b64encode", 0, OPTION_B64ENCODE },
718029ab02SPeter Avalos { "block-size", 1, 'b' },
72e95abc47Szrj { "blocking-factor", 1, 'b' },
738029ab02SPeter Avalos { "bunzip2", 0, 'j' },
748029ab02SPeter Avalos { "bzip", 0, 'j' },
758029ab02SPeter Avalos { "bzip2", 0, 'j' },
768029ab02SPeter Avalos { "cd", 1, 'C' },
778029ab02SPeter Avalos { "check-links", 0, OPTION_CHECK_LINKS },
788029ab02SPeter Avalos { "chroot", 0, OPTION_CHROOT },
796b384f39SPeter Avalos { "clear-nochange-fflags", 0, OPTION_CLEAR_NOCHANGE_FFLAGS },
808029ab02SPeter Avalos { "compress", 0, 'Z' },
818029ab02SPeter Avalos { "confirmation", 0, 'w' },
828029ab02SPeter Avalos { "create", 0, 'c' },
838029ab02SPeter Avalos { "dereference", 0, 'L' },
848029ab02SPeter Avalos { "directory", 1, 'C' },
85e95abc47Szrj { "disable-copyfile", 0, OPTION_NO_MAC_METADATA },
868029ab02SPeter Avalos { "exclude", 1, OPTION_EXCLUDE },
878029ab02SPeter Avalos { "exclude-from", 1, 'X' },
88085658deSDaniel Fojt { "exclude-vcs", 0, OPTION_EXCLUDE_VCS },
898029ab02SPeter Avalos { "extract", 0, 'x' },
908029ab02SPeter Avalos { "fast-read", 0, 'q' },
91e95abc47Szrj { "fflags", 0, OPTION_FFLAGS },
928029ab02SPeter Avalos { "file", 1, 'f' },
938029ab02SPeter Avalos { "files-from", 1, 'T' },
948029ab02SPeter Avalos { "format", 1, OPTION_FORMAT },
95c09f92d2SPeter Avalos { "gid", 1, OPTION_GID },
96c09f92d2SPeter Avalos { "gname", 1, OPTION_GNAME },
97d4d8193eSPeter Avalos { "grzip", 0, OPTION_GRZIP },
988029ab02SPeter Avalos { "gunzip", 0, 'z' },
998029ab02SPeter Avalos { "gzip", 0, 'z' },
1008029ab02SPeter Avalos { "help", 0, OPTION_HELP },
101d4d8193eSPeter Avalos { "hfsCompression", 0, OPTION_HFS_COMPRESSION },
1026b384f39SPeter Avalos { "ignore-zeros", 0, OPTION_IGNORE_ZEROS },
1038029ab02SPeter Avalos { "include", 1, OPTION_INCLUDE },
1048029ab02SPeter Avalos { "insecure", 0, 'P' },
105c09f92d2SPeter Avalos { "interactive", 0, 'w' },
1068029ab02SPeter Avalos { "keep-newer-files", 0, OPTION_KEEP_NEWER_FILES },
1078029ab02SPeter Avalos { "keep-old-files", 0, 'k' },
1088029ab02SPeter Avalos { "list", 0, 't' },
109d4d8193eSPeter Avalos { "lrzip", 0, OPTION_LRZIP },
1106b384f39SPeter Avalos { "lz4", 0, OPTION_LZ4 },
111c09f92d2SPeter Avalos { "lzip", 0, OPTION_LZIP },
1128029ab02SPeter Avalos { "lzma", 0, OPTION_LZMA },
113d4d8193eSPeter Avalos { "lzop", 0, OPTION_LZOP },
114e95abc47Szrj { "mac-metadata", 0, OPTION_MAC_METADATA },
1158029ab02SPeter Avalos { "modification-time", 0, 'm' },
1168029ab02SPeter Avalos { "newer", 1, OPTION_NEWER_CTIME },
1178029ab02SPeter Avalos { "newer-ctime", 1, OPTION_NEWER_CTIME },
1188029ab02SPeter Avalos { "newer-ctime-than", 1, OPTION_NEWER_CTIME_THAN },
1198029ab02SPeter Avalos { "newer-mtime", 1, OPTION_NEWER_MTIME },
1208029ab02SPeter Avalos { "newer-mtime-than", 1, OPTION_NEWER_MTIME_THAN },
1218029ab02SPeter Avalos { "newer-than", 1, OPTION_NEWER_CTIME_THAN },
122e95abc47Szrj { "no-acls", 0, OPTION_NO_ACLS },
123e95abc47Szrj { "no-fflags", 0, OPTION_NO_FFLAGS },
124e95abc47Szrj { "no-mac-metadata", 0, OPTION_NO_MAC_METADATA },
125*50f8aa9cSAntonio Huete Jimenez { "no-read-sparse", 0, OPTION_NO_READ_SPARSE },
1268029ab02SPeter Avalos { "no-recursion", 0, 'n' },
127085658deSDaniel Fojt { "no-safe-writes", 0, OPTION_NO_SAFE_WRITES },
1288029ab02SPeter Avalos { "no-same-owner", 0, OPTION_NO_SAME_OWNER },
1298029ab02SPeter Avalos { "no-same-permissions", 0, OPTION_NO_SAME_PERMISSIONS },
130e95abc47Szrj { "no-xattr", 0, OPTION_NO_XATTRS },
131e95abc47Szrj { "no-xattrs", 0, OPTION_NO_XATTRS },
132c09f92d2SPeter Avalos { "nodump", 0, OPTION_NODUMP },
133d4d8193eSPeter Avalos { "nopreserveHFSCompression",0, OPTION_NOPRESERVE_HFS_COMPRESSION },
134c09f92d2SPeter Avalos { "norecurse", 0, 'n' },
1358029ab02SPeter Avalos { "null", 0, OPTION_NULL },
1368029ab02SPeter Avalos { "numeric-owner", 0, OPTION_NUMERIC_OWNER },
137d4d8193eSPeter Avalos { "older", 1, OPTION_OLDER_CTIME },
138d4d8193eSPeter Avalos { "older-ctime", 1, OPTION_OLDER_CTIME },
139d4d8193eSPeter Avalos { "older-ctime-than", 1, OPTION_OLDER_CTIME_THAN },
140d4d8193eSPeter Avalos { "older-mtime", 1, OPTION_OLDER_MTIME },
141d4d8193eSPeter Avalos { "older-mtime-than", 1, OPTION_OLDER_MTIME_THAN },
142d4d8193eSPeter Avalos { "older-than", 1, OPTION_OLDER_CTIME_THAN },
1438029ab02SPeter Avalos { "one-file-system", 0, OPTION_ONE_FILE_SYSTEM },
144c09f92d2SPeter Avalos { "options", 1, OPTION_OPTIONS },
1456b384f39SPeter Avalos { "passphrase", 1, OPTION_PASSPHRASE },
1468029ab02SPeter Avalos { "posix", 0, OPTION_POSIX },
1478029ab02SPeter Avalos { "preserve-permissions", 0, 'p' },
1488029ab02SPeter Avalos { "read-full-blocks", 0, 'B' },
149*50f8aa9cSAntonio Huete Jimenez { "read-sparse", 0, OPTION_READ_SPARSE },
150085658deSDaniel Fojt { "safe-writes", 0, OPTION_SAFE_WRITES },
1518029ab02SPeter Avalos { "same-owner", 0, OPTION_SAME_OWNER },
1528029ab02SPeter Avalos { "same-permissions", 0, 'p' },
1538029ab02SPeter Avalos { "strip-components", 1, OPTION_STRIP_COMPONENTS },
1548029ab02SPeter Avalos { "to-stdout", 0, 'O' },
1558029ab02SPeter Avalos { "totals", 0, OPTION_TOTALS },
156c09f92d2SPeter Avalos { "uid", 1, OPTION_UID },
157c09f92d2SPeter Avalos { "uname", 1, OPTION_UNAME },
1588029ab02SPeter Avalos { "uncompress", 0, 'Z' },
1598029ab02SPeter Avalos { "unlink", 0, 'U' },
1608029ab02SPeter Avalos { "unlink-first", 0, 'U' },
1618029ab02SPeter Avalos { "update", 0, 'u' },
1628029ab02SPeter Avalos { "use-compress-program", 1, OPTION_USE_COMPRESS_PROGRAM },
163d4d8193eSPeter Avalos { "uuencode", 0, OPTION_UUENCODE },
1648029ab02SPeter Avalos { "verbose", 0, 'v' },
1658029ab02SPeter Avalos { "version", 0, OPTION_VERSION },
166e95abc47Szrj { "xattrs", 0, OPTION_XATTRS },
1678029ab02SPeter Avalos { "xz", 0, 'J' },
168e95abc47Szrj { "zstd", 0, OPTION_ZSTD },
1698029ab02SPeter Avalos { NULL, 0, 0 }
1708029ab02SPeter Avalos };
1718029ab02SPeter Avalos
1728029ab02SPeter Avalos /*
1738029ab02SPeter Avalos * This getopt implementation has two key features that common
1748029ab02SPeter Avalos * getopt_long() implementations lack. Apart from those, it's a
1758029ab02SPeter Avalos * straightforward option parser, considerably simplified by not
1768029ab02SPeter Avalos * needing to support the wealth of exotic getopt_long() features. It
1778029ab02SPeter Avalos * has, of course, been shamelessly tailored for bsdtar. (If you're
1788029ab02SPeter Avalos * looking for a generic getopt_long() implementation for your
1798029ab02SPeter Avalos * project, I recommend Gregory Pietsch's public domain getopt_long()
1808029ab02SPeter Avalos * implementation.) The two additional features are:
1818029ab02SPeter Avalos *
1828029ab02SPeter Avalos * Old-style tar arguments: The original tar implementation treated
1838029ab02SPeter Avalos * the first argument word as a list of single-character option
1848029ab02SPeter Avalos * letters. All arguments follow as separate words. For example,
1858029ab02SPeter Avalos * tar xbf 32 /dev/tape
1868029ab02SPeter Avalos * Here, the "xbf" is three option letters, "32" is the argument for
1878029ab02SPeter Avalos * "b" and "/dev/tape" is the argument for "f". We support this usage
1888029ab02SPeter Avalos * if the first command-line argument does not begin with '-'. We
1898029ab02SPeter Avalos * also allow regular short and long options to follow, e.g.,
1908029ab02SPeter Avalos * tar xbf 32 /dev/tape -P --format=pax
1918029ab02SPeter Avalos *
1928029ab02SPeter Avalos * -W long options: There's an obscure GNU convention (only rarely
1938029ab02SPeter Avalos * supported even there) that allows "-W option=argument" as an
1948029ab02SPeter Avalos * alternative way to support long options. This was supported in
1958029ab02SPeter Avalos * early bsdtar as a way to access long options on platforms that did
1968029ab02SPeter Avalos * not support getopt_long() and is preserved here for backwards
1978029ab02SPeter Avalos * compatibility. (Of course, if I'd started with a custom
1988029ab02SPeter Avalos * command-line parser from the beginning, I would have had normal
1998029ab02SPeter Avalos * long option support on every platform so that hack wouldn't have
2008029ab02SPeter Avalos * been necessary. Oh, well. Some mistakes you just have to live
2018029ab02SPeter Avalos * with.)
2028029ab02SPeter Avalos *
2038029ab02SPeter Avalos * TODO: We should be able to use this to pull files and intermingled
2048029ab02SPeter Avalos * options (such as -C) from the command line in write mode. That
2058029ab02SPeter Avalos * will require a little rethinking of the argument handling in
2068029ab02SPeter Avalos * bsdtar.c.
2078029ab02SPeter Avalos *
2088029ab02SPeter Avalos * TODO: If we want to support arbitrary command-line options from -T
2098029ab02SPeter Avalos * input (as GNU tar does), we may need to extend this to handle option
210c09f92d2SPeter Avalos * words from sources other than argv/argc. I'm not really sure if I
2118029ab02SPeter Avalos * like that feature of GNU tar, so it's certainly not a priority.
2128029ab02SPeter Avalos */
2138029ab02SPeter Avalos
2148029ab02SPeter Avalos int
bsdtar_getopt(struct bsdtar * bsdtar)2158029ab02SPeter Avalos bsdtar_getopt(struct bsdtar *bsdtar)
2168029ab02SPeter Avalos {
2178029ab02SPeter Avalos enum { state_start = 0, state_old_tar, state_next_word,
2188029ab02SPeter Avalos state_short, state_long };
2198029ab02SPeter Avalos
220c09f92d2SPeter Avalos const struct bsdtar_option *popt, *match = NULL, *match2 = NULL;
2218029ab02SPeter Avalos const char *p, *long_prefix = "--";
2228029ab02SPeter Avalos size_t optlength;
2238029ab02SPeter Avalos int opt = '?';
2248029ab02SPeter Avalos int required = 0;
2258029ab02SPeter Avalos
226c09f92d2SPeter Avalos bsdtar->argument = NULL;
2278029ab02SPeter Avalos
2288029ab02SPeter Avalos /* First time through, initialize everything. */
229c09f92d2SPeter Avalos if (bsdtar->getopt_state == state_start) {
2308029ab02SPeter Avalos /* Skip program name. */
2318029ab02SPeter Avalos ++bsdtar->argv;
2328029ab02SPeter Avalos --bsdtar->argc;
2338029ab02SPeter Avalos if (*bsdtar->argv == NULL)
2348029ab02SPeter Avalos return (-1);
2358029ab02SPeter Avalos /* Decide between "new style" and "old style" arguments. */
2368029ab02SPeter Avalos if (bsdtar->argv[0][0] == '-') {
237c09f92d2SPeter Avalos bsdtar->getopt_state = state_next_word;
2388029ab02SPeter Avalos } else {
239c09f92d2SPeter Avalos bsdtar->getopt_state = state_old_tar;
240c09f92d2SPeter Avalos bsdtar->getopt_word = *bsdtar->argv++;
2418029ab02SPeter Avalos --bsdtar->argc;
2428029ab02SPeter Avalos }
2438029ab02SPeter Avalos }
2448029ab02SPeter Avalos
2458029ab02SPeter Avalos /*
2468029ab02SPeter Avalos * We're parsing old-style tar arguments
2478029ab02SPeter Avalos */
248c09f92d2SPeter Avalos if (bsdtar->getopt_state == state_old_tar) {
2498029ab02SPeter Avalos /* Get the next option character. */
250c09f92d2SPeter Avalos opt = *bsdtar->getopt_word++;
2518029ab02SPeter Avalos if (opt == '\0') {
2528029ab02SPeter Avalos /* New-style args can follow old-style. */
253c09f92d2SPeter Avalos bsdtar->getopt_state = state_next_word;
2548029ab02SPeter Avalos } else {
2558029ab02SPeter Avalos /* See if it takes an argument. */
2568029ab02SPeter Avalos p = strchr(short_options, opt);
2578029ab02SPeter Avalos if (p == NULL)
2588029ab02SPeter Avalos return ('?');
2598029ab02SPeter Avalos if (p[1] == ':') {
260c09f92d2SPeter Avalos bsdtar->argument = *bsdtar->argv;
261c09f92d2SPeter Avalos if (bsdtar->argument == NULL) {
2629c82a63eSPeter Avalos lafe_warnc(0,
2638029ab02SPeter Avalos "Option %c requires an argument",
2648029ab02SPeter Avalos opt);
2658029ab02SPeter Avalos return ('?');
2668029ab02SPeter Avalos }
2678029ab02SPeter Avalos ++bsdtar->argv;
2688029ab02SPeter Avalos --bsdtar->argc;
2698029ab02SPeter Avalos }
2708029ab02SPeter Avalos }
2718029ab02SPeter Avalos }
2728029ab02SPeter Avalos
2738029ab02SPeter Avalos /*
2748029ab02SPeter Avalos * We're ready to look at the next word in argv.
2758029ab02SPeter Avalos */
276c09f92d2SPeter Avalos if (bsdtar->getopt_state == state_next_word) {
2778029ab02SPeter Avalos /* No more arguments, so no more options. */
2788029ab02SPeter Avalos if (bsdtar->argv[0] == NULL)
2798029ab02SPeter Avalos return (-1);
2808029ab02SPeter Avalos /* Doesn't start with '-', so no more options. */
2818029ab02SPeter Avalos if (bsdtar->argv[0][0] != '-')
2828029ab02SPeter Avalos return (-1);
2838029ab02SPeter Avalos /* "--" marks end of options; consume it and return. */
2848029ab02SPeter Avalos if (strcmp(bsdtar->argv[0], "--") == 0) {
2858029ab02SPeter Avalos ++bsdtar->argv;
2868029ab02SPeter Avalos --bsdtar->argc;
2878029ab02SPeter Avalos return (-1);
2888029ab02SPeter Avalos }
2898029ab02SPeter Avalos /* Get next word for parsing. */
290c09f92d2SPeter Avalos bsdtar->getopt_word = *bsdtar->argv++;
2918029ab02SPeter Avalos --bsdtar->argc;
292c09f92d2SPeter Avalos if (bsdtar->getopt_word[1] == '-') {
2938029ab02SPeter Avalos /* Set up long option parser. */
294c09f92d2SPeter Avalos bsdtar->getopt_state = state_long;
295c09f92d2SPeter Avalos bsdtar->getopt_word += 2; /* Skip leading '--' */
2968029ab02SPeter Avalos } else {
2978029ab02SPeter Avalos /* Set up short option parser. */
298c09f92d2SPeter Avalos bsdtar->getopt_state = state_short;
299c09f92d2SPeter Avalos ++bsdtar->getopt_word; /* Skip leading '-' */
3008029ab02SPeter Avalos }
3018029ab02SPeter Avalos }
3028029ab02SPeter Avalos
3038029ab02SPeter Avalos /*
3048029ab02SPeter Avalos * We're parsing a group of POSIX-style single-character options.
3058029ab02SPeter Avalos */
306c09f92d2SPeter Avalos if (bsdtar->getopt_state == state_short) {
3078029ab02SPeter Avalos /* Peel next option off of a group of short options. */
308c09f92d2SPeter Avalos opt = *bsdtar->getopt_word++;
3098029ab02SPeter Avalos if (opt == '\0') {
3108029ab02SPeter Avalos /* End of this group; recurse to get next option. */
311c09f92d2SPeter Avalos bsdtar->getopt_state = state_next_word;
3128029ab02SPeter Avalos return bsdtar_getopt(bsdtar);
3138029ab02SPeter Avalos }
3148029ab02SPeter Avalos
3158029ab02SPeter Avalos /* Does this option take an argument? */
3168029ab02SPeter Avalos p = strchr(short_options, opt);
3178029ab02SPeter Avalos if (p == NULL)
3188029ab02SPeter Avalos return ('?');
3198029ab02SPeter Avalos if (p[1] == ':')
3208029ab02SPeter Avalos required = 1;
3218029ab02SPeter Avalos
3228029ab02SPeter Avalos /* If it takes an argument, parse that. */
3238029ab02SPeter Avalos if (required) {
324c09f92d2SPeter Avalos /* If arg is run-in, bsdtar->getopt_word already points to it. */
325c09f92d2SPeter Avalos if (bsdtar->getopt_word[0] == '\0') {
3268029ab02SPeter Avalos /* Otherwise, pick up the next word. */
327c09f92d2SPeter Avalos bsdtar->getopt_word = *bsdtar->argv;
328c09f92d2SPeter Avalos if (bsdtar->getopt_word == NULL) {
3299c82a63eSPeter Avalos lafe_warnc(0,
3308029ab02SPeter Avalos "Option -%c requires an argument",
3318029ab02SPeter Avalos opt);
3328029ab02SPeter Avalos return ('?');
3338029ab02SPeter Avalos }
3348029ab02SPeter Avalos ++bsdtar->argv;
3358029ab02SPeter Avalos --bsdtar->argc;
3368029ab02SPeter Avalos }
3378029ab02SPeter Avalos if (opt == 'W') {
338c09f92d2SPeter Avalos bsdtar->getopt_state = state_long;
3398029ab02SPeter Avalos long_prefix = "-W "; /* For clearer errors. */
3408029ab02SPeter Avalos } else {
341c09f92d2SPeter Avalos bsdtar->getopt_state = state_next_word;
342c09f92d2SPeter Avalos bsdtar->argument = bsdtar->getopt_word;
3438029ab02SPeter Avalos }
3448029ab02SPeter Avalos }
3458029ab02SPeter Avalos }
3468029ab02SPeter Avalos
3478029ab02SPeter Avalos /* We're reading a long option, including -W long=arg convention. */
348c09f92d2SPeter Avalos if (bsdtar->getopt_state == state_long) {
3498029ab02SPeter Avalos /* After this long option, we'll be starting a new word. */
350c09f92d2SPeter Avalos bsdtar->getopt_state = state_next_word;
3518029ab02SPeter Avalos
3528029ab02SPeter Avalos /* Option name ends at '=' if there is one. */
353c09f92d2SPeter Avalos p = strchr(bsdtar->getopt_word, '=');
3548029ab02SPeter Avalos if (p != NULL) {
355c09f92d2SPeter Avalos optlength = (size_t)(p - bsdtar->getopt_word);
356c09f92d2SPeter Avalos bsdtar->argument = (char *)(uintptr_t)(p + 1);
3578029ab02SPeter Avalos } else {
358c09f92d2SPeter Avalos optlength = strlen(bsdtar->getopt_word);
3598029ab02SPeter Avalos }
3608029ab02SPeter Avalos
3618029ab02SPeter Avalos /* Search the table for an unambiguous match. */
3628029ab02SPeter Avalos for (popt = tar_longopts; popt->name != NULL; popt++) {
3638029ab02SPeter Avalos /* Short-circuit if first chars don't match. */
364c09f92d2SPeter Avalos if (popt->name[0] != bsdtar->getopt_word[0])
3658029ab02SPeter Avalos continue;
3668029ab02SPeter Avalos /* If option is a prefix of name in table, record it.*/
367c09f92d2SPeter Avalos if (strncmp(bsdtar->getopt_word, popt->name, optlength) == 0) {
3688029ab02SPeter Avalos match2 = match; /* Record up to two matches. */
3698029ab02SPeter Avalos match = popt;
3708029ab02SPeter Avalos /* If it's an exact match, we're done. */
3718029ab02SPeter Avalos if (strlen(popt->name) == optlength) {
3728029ab02SPeter Avalos match2 = NULL; /* Forget the others. */
3738029ab02SPeter Avalos break;
3748029ab02SPeter Avalos }
3758029ab02SPeter Avalos }
3768029ab02SPeter Avalos }
3778029ab02SPeter Avalos
3788029ab02SPeter Avalos /* Fail if there wasn't a unique match. */
3798029ab02SPeter Avalos if (match == NULL) {
3809c82a63eSPeter Avalos lafe_warnc(0,
3818029ab02SPeter Avalos "Option %s%s is not supported",
382c09f92d2SPeter Avalos long_prefix, bsdtar->getopt_word);
3838029ab02SPeter Avalos return ('?');
3848029ab02SPeter Avalos }
3858029ab02SPeter Avalos if (match2 != NULL) {
3869c82a63eSPeter Avalos lafe_warnc(0,
3878029ab02SPeter Avalos "Ambiguous option %s%s (matches --%s and --%s)",
388c09f92d2SPeter Avalos long_prefix, bsdtar->getopt_word, match->name, match2->name);
3898029ab02SPeter Avalos return ('?');
3908029ab02SPeter Avalos }
3918029ab02SPeter Avalos
3928029ab02SPeter Avalos /* We've found a unique match; does it need an argument? */
3938029ab02SPeter Avalos if (match->required) {
3948029ab02SPeter Avalos /* Argument required: get next word if necessary. */
395c09f92d2SPeter Avalos if (bsdtar->argument == NULL) {
396c09f92d2SPeter Avalos bsdtar->argument = *bsdtar->argv;
397c09f92d2SPeter Avalos if (bsdtar->argument == NULL) {
3989c82a63eSPeter Avalos lafe_warnc(0,
3998029ab02SPeter Avalos "Option %s%s requires an argument",
4008029ab02SPeter Avalos long_prefix, match->name);
4018029ab02SPeter Avalos return ('?');
4028029ab02SPeter Avalos }
4038029ab02SPeter Avalos ++bsdtar->argv;
4048029ab02SPeter Avalos --bsdtar->argc;
4058029ab02SPeter Avalos }
4068029ab02SPeter Avalos } else {
4078029ab02SPeter Avalos /* Argument forbidden: fail if there is one. */
408c09f92d2SPeter Avalos if (bsdtar->argument != NULL) {
4099c82a63eSPeter Avalos lafe_warnc(0,
4108029ab02SPeter Avalos "Option %s%s does not allow an argument",
4118029ab02SPeter Avalos long_prefix, match->name);
4128029ab02SPeter Avalos return ('?');
4138029ab02SPeter Avalos }
4148029ab02SPeter Avalos }
4158029ab02SPeter Avalos return (match->equivalent);
4168029ab02SPeter Avalos }
4178029ab02SPeter Avalos
4188029ab02SPeter Avalos return (opt);
4198029ab02SPeter Avalos }
420