1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2003-2008 Tim Kientzle
5 * All rights reserved.
6 */
7
8 /*
9 * Command line parser for tar.
10 */
11
12 #include "bsdtar_platform.h"
13
14 #ifdef HAVE_ERRNO_H
15 #include <errno.h>
16 #endif
17 #ifdef HAVE_STDLIB_H
18 #include <stdlib.h>
19 #endif
20 #ifdef HAVE_STRING_H
21 #include <string.h>
22 #endif
23
24 #include "bsdtar.h"
25 #include "err.h"
26
27 /*
28 * Short options for tar. Please keep this sorted.
29 */
30 static const char *short_options
31 = "aBb:C:cf:HhI:JjkLlmnOoPpqrSs:T:tUuvW:wX:xyZz";
32
33 /*
34 * Long options for tar. Please keep this list sorted.
35 *
36 * The symbolic names for options that lack a short equivalent are
37 * defined in bsdtar.h. Also note that so far I've found no need
38 * to support optional arguments to long options. That would be
39 * a small change to the code below.
40 */
41
42 static const struct bsdtar_option {
43 const char *name;
44 int required; /* 1 if this option requires an argument. */
45 int equivalent; /* Equivalent short option. */
46 } tar_longopts[] = {
47 { "absolute-paths", 0, 'P' },
48 { "append", 0, 'r' },
49 { "acls", 0, OPTION_ACLS },
50 { "auto-compress", 0, 'a' },
51 { "b64encode", 0, OPTION_B64ENCODE },
52 { "block-size", 1, 'b' },
53 { "blocking-factor", 1, 'b' },
54 { "bunzip2", 0, 'j' },
55 { "bzip", 0, 'j' },
56 { "bzip2", 0, 'j' },
57 { "cd", 1, 'C' },
58 { "check-links", 0, OPTION_CHECK_LINKS },
59 { "chroot", 0, OPTION_CHROOT },
60 { "clamp-mtime", 0, OPTION_CLAMP_MTIME },
61 { "clear-nochange-fflags", 0, OPTION_CLEAR_NOCHANGE_FFLAGS },
62 { "compress", 0, 'Z' },
63 { "confirmation", 0, 'w' },
64 { "create", 0, 'c' },
65 { "dereference", 0, 'L' },
66 { "directory", 1, 'C' },
67 { "disable-copyfile", 0, OPTION_NO_MAC_METADATA },
68 { "exclude", 1, OPTION_EXCLUDE },
69 { "exclude-from", 1, 'X' },
70 { "exclude-vcs", 0, OPTION_EXCLUDE_VCS },
71 { "extract", 0, 'x' },
72 { "fast-read", 0, 'q' },
73 { "fflags", 0, OPTION_FFLAGS },
74 { "file", 1, 'f' },
75 { "files-from", 1, 'T' },
76 { "format", 1, OPTION_FORMAT },
77 { "gid", 1, OPTION_GID },
78 { "gname", 1, OPTION_GNAME },
79 { "group", 1, OPTION_GROUP },
80 { "grzip", 0, OPTION_GRZIP },
81 { "gunzip", 0, 'z' },
82 { "gzip", 0, 'z' },
83 { "help", 0, OPTION_HELP },
84 { "hfsCompression", 0, OPTION_HFS_COMPRESSION },
85 { "ignore-zeros", 0, OPTION_IGNORE_ZEROS },
86 { "include", 1, OPTION_INCLUDE },
87 { "insecure", 0, 'P' },
88 { "interactive", 0, 'w' },
89 { "keep-newer-files", 0, OPTION_KEEP_NEWER_FILES },
90 { "keep-old-files", 0, 'k' },
91 { "list", 0, 't' },
92 { "lrzip", 0, OPTION_LRZIP },
93 { "lz4", 0, OPTION_LZ4 },
94 { "lzip", 0, OPTION_LZIP },
95 { "lzma", 0, OPTION_LZMA },
96 { "lzop", 0, OPTION_LZOP },
97 { "mac-metadata", 0, OPTION_MAC_METADATA },
98 { "modification-time", 0, 'm' },
99 { "mtime", 1, OPTION_MTIME },
100 { "newer", 1, OPTION_NEWER_CTIME },
101 { "newer-ctime", 1, OPTION_NEWER_CTIME },
102 { "newer-ctime-than", 1, OPTION_NEWER_CTIME_THAN },
103 { "newer-mtime", 1, OPTION_NEWER_MTIME },
104 { "newer-mtime-than", 1, OPTION_NEWER_MTIME_THAN },
105 { "newer-than", 1, OPTION_NEWER_CTIME_THAN },
106 { "no-acls", 0, OPTION_NO_ACLS },
107 { "no-fflags", 0, OPTION_NO_FFLAGS },
108 { "no-mac-metadata", 0, OPTION_NO_MAC_METADATA },
109 { "no-read-sparse", 0, OPTION_NO_READ_SPARSE },
110 { "no-recursion", 0, 'n' },
111 { "no-safe-writes", 0, OPTION_NO_SAFE_WRITES },
112 { "no-same-owner", 0, OPTION_NO_SAME_OWNER },
113 { "no-same-permissions", 0, OPTION_NO_SAME_PERMISSIONS },
114 { "no-xattr", 0, OPTION_NO_XATTRS },
115 { "no-xattrs", 0, OPTION_NO_XATTRS },
116 { "nodump", 0, OPTION_NODUMP },
117 { "nopreserveHFSCompression",0, OPTION_NOPRESERVE_HFS_COMPRESSION },
118 { "norecurse", 0, 'n' },
119 { "null", 0, OPTION_NULL },
120 { "numeric-owner", 0, OPTION_NUMERIC_OWNER },
121 { "older", 1, OPTION_OLDER_CTIME },
122 { "older-ctime", 1, OPTION_OLDER_CTIME },
123 { "older-ctime-than", 1, OPTION_OLDER_CTIME_THAN },
124 { "older-mtime", 1, OPTION_OLDER_MTIME },
125 { "older-mtime-than", 1, OPTION_OLDER_MTIME_THAN },
126 { "older-than", 1, OPTION_OLDER_CTIME_THAN },
127 { "one-file-system", 0, OPTION_ONE_FILE_SYSTEM },
128 { "options", 1, OPTION_OPTIONS },
129 { "owner", 1, OPTION_OWNER },
130 { "passphrase", 1, OPTION_PASSPHRASE },
131 { "posix", 0, OPTION_POSIX },
132 { "preserve-permissions", 0, 'p' },
133 { "read-full-blocks", 0, 'B' },
134 { "read-sparse", 0, OPTION_READ_SPARSE },
135 { "safe-writes", 0, OPTION_SAFE_WRITES },
136 { "same-owner", 0, OPTION_SAME_OWNER },
137 { "same-permissions", 0, 'p' },
138 { "strip-components", 1, OPTION_STRIP_COMPONENTS },
139 { "to-stdout", 0, 'O' },
140 { "totals", 0, OPTION_TOTALS },
141 { "uid", 1, OPTION_UID },
142 { "uname", 1, OPTION_UNAME },
143 { "uncompress", 0, 'Z' },
144 { "unlink", 0, 'U' },
145 { "unlink-first", 0, 'U' },
146 { "update", 0, 'u' },
147 { "use-compress-program", 1, OPTION_USE_COMPRESS_PROGRAM },
148 { "uuencode", 0, OPTION_UUENCODE },
149 { "verbose", 0, 'v' },
150 { "version", 0, OPTION_VERSION },
151 { "xattrs", 0, OPTION_XATTRS },
152 { "xz", 0, 'J' },
153 { "zstd", 0, OPTION_ZSTD },
154 { NULL, 0, 0 }
155 };
156
157 /*
158 * This getopt implementation has two key features that common
159 * getopt_long() implementations lack. Apart from those, it's a
160 * straightforward option parser, considerably simplified by not
161 * needing to support the wealth of exotic getopt_long() features. It
162 * has, of course, been shamelessly tailored for bsdtar. (If you're
163 * looking for a generic getopt_long() implementation for your
164 * project, I recommend Gregory Pietsch's public domain getopt_long()
165 * implementation.) The two additional features are:
166 *
167 * Old-style tar arguments: The original tar implementation treated
168 * the first argument word as a list of single-character option
169 * letters. All arguments follow as separate words. For example,
170 * tar xbf 32 /dev/tape
171 * Here, the "xbf" is three option letters, "32" is the argument for
172 * "b" and "/dev/tape" is the argument for "f". We support this usage
173 * if the first command-line argument does not begin with '-'. We
174 * also allow regular short and long options to follow, e.g.,
175 * tar xbf 32 /dev/tape -P --format=pax
176 *
177 * -W long options: There's an obscure GNU convention (only rarely
178 * supported even there) that allows "-W option=argument" as an
179 * alternative way to support long options. This was supported in
180 * early bsdtar as a way to access long options on platforms that did
181 * not support getopt_long() and is preserved here for backwards
182 * compatibility. (Of course, if I'd started with a custom
183 * command-line parser from the beginning, I would have had normal
184 * long option support on every platform so that hack wouldn't have
185 * been necessary. Oh, well. Some mistakes you just have to live
186 * with.)
187 *
188 * TODO: We should be able to use this to pull files and intermingled
189 * options (such as -C) from the command line in write mode. That
190 * will require a little rethinking of the argument handling in
191 * bsdtar.c.
192 *
193 * TODO: If we want to support arbitrary command-line options from -T
194 * input (as GNU tar does), we may need to extend this to handle option
195 * words from sources other than argv/argc. I'm not really sure if I
196 * like that feature of GNU tar, so it's certainly not a priority.
197 */
198
199 int
bsdtar_getopt(struct bsdtar * bsdtar)200 bsdtar_getopt(struct bsdtar *bsdtar)
201 {
202 enum { state_start = 0, state_old_tar, state_next_word,
203 state_short, state_long };
204
205 const struct bsdtar_option *popt, *match, *match2;
206 const char *p, *long_prefix;
207 size_t optlength;
208 int opt;
209 int required;
210
211 again:
212 match = NULL;
213 match2 = NULL;
214 long_prefix = "--";
215 opt = '?';
216 required = 0;
217 bsdtar->argument = NULL;
218
219 /* First time through, initialize everything. */
220 if (bsdtar->getopt_state == state_start) {
221 /* Skip program name. */
222 ++bsdtar->argv;
223 --bsdtar->argc;
224 if (*bsdtar->argv == NULL)
225 return (-1);
226 /* Decide between "new style" and "old style" arguments. */
227 if (bsdtar->argv[0][0] == '-') {
228 bsdtar->getopt_state = state_next_word;
229 } else {
230 bsdtar->getopt_state = state_old_tar;
231 bsdtar->getopt_word = *bsdtar->argv++;
232 --bsdtar->argc;
233 }
234 }
235
236 /*
237 * We're parsing old-style tar arguments
238 */
239 if (bsdtar->getopt_state == state_old_tar) {
240 /* Get the next option character. */
241 opt = *bsdtar->getopt_word++;
242 if (opt == '\0') {
243 /* New-style args can follow old-style. */
244 bsdtar->getopt_state = state_next_word;
245 } else {
246 /* See if it takes an argument. */
247 p = strchr(short_options, opt);
248 if (p == NULL)
249 return ('?');
250 if (p[1] == ':') {
251 bsdtar->argument = *bsdtar->argv;
252 if (bsdtar->argument == NULL) {
253 lafe_warnc(0,
254 "Option %c requires an argument",
255 opt);
256 return ('?');
257 }
258 ++bsdtar->argv;
259 --bsdtar->argc;
260 }
261 }
262 }
263
264 /*
265 * We're ready to look at the next word in argv.
266 */
267 if (bsdtar->getopt_state == state_next_word) {
268 /* No more arguments, so no more options. */
269 if (bsdtar->argv[0] == NULL)
270 return (-1);
271 /* Doesn't start with '-', so no more options. */
272 if (bsdtar->argv[0][0] != '-')
273 return (-1);
274 /* "--" marks end of options; consume it and return. */
275 if (strcmp(bsdtar->argv[0], "--") == 0) {
276 ++bsdtar->argv;
277 --bsdtar->argc;
278 return (-1);
279 }
280 /* Get next word for parsing. */
281 bsdtar->getopt_word = *bsdtar->argv++;
282 --bsdtar->argc;
283 if (bsdtar->getopt_word[1] == '-') {
284 /* Set up long option parser. */
285 bsdtar->getopt_state = state_long;
286 bsdtar->getopt_word += 2; /* Skip leading '--' */
287 } else {
288 /* Set up short option parser. */
289 bsdtar->getopt_state = state_short;
290 ++bsdtar->getopt_word; /* Skip leading '-' */
291 }
292 }
293
294 /*
295 * We're parsing a group of POSIX-style single-character options.
296 */
297 if (bsdtar->getopt_state == state_short) {
298 /* Peel next option off of a group of short options. */
299 opt = *bsdtar->getopt_word++;
300 if (opt == '\0') {
301 /* End of this group; recurse to get next option. */
302 bsdtar->getopt_state = state_next_word;
303 goto again;
304 }
305
306 /* Does this option take an argument? */
307 p = strchr(short_options, opt);
308 if (p == NULL)
309 return ('?');
310 if (p[1] == ':')
311 required = 1;
312
313 /* If it takes an argument, parse that. */
314 if (required) {
315 /* If arg is run-in, bsdtar->getopt_word already points to it. */
316 if (bsdtar->getopt_word[0] == '\0') {
317 /* Otherwise, pick up the next word. */
318 bsdtar->getopt_word = *bsdtar->argv;
319 if (bsdtar->getopt_word == NULL) {
320 lafe_warnc(0,
321 "Option -%c requires an argument",
322 opt);
323 return ('?');
324 }
325 ++bsdtar->argv;
326 --bsdtar->argc;
327 }
328 if (opt == 'W') {
329 bsdtar->getopt_state = state_long;
330 long_prefix = "-W "; /* For clearer errors. */
331 } else {
332 bsdtar->getopt_state = state_next_word;
333 bsdtar->argument = bsdtar->getopt_word;
334 }
335 }
336 }
337
338 /* We're reading a long option, including -W long=arg convention. */
339 if (bsdtar->getopt_state == state_long) {
340 /* After this long option, we'll be starting a new word. */
341 bsdtar->getopt_state = state_next_word;
342
343 /* Option name ends at '=' if there is one. */
344 p = strchr(bsdtar->getopt_word, '=');
345 if (p != NULL) {
346 optlength = (size_t)(p - bsdtar->getopt_word);
347 bsdtar->argument = (char *)(uintptr_t)(p + 1);
348 } else {
349 optlength = strlen(bsdtar->getopt_word);
350 }
351
352 /* Search the table for an unambiguous match. */
353 for (popt = tar_longopts; popt->name != NULL; popt++) {
354 /* Short-circuit if first chars don't match. */
355 if (popt->name[0] != bsdtar->getopt_word[0])
356 continue;
357 /* If option is a prefix of name in table, record it.*/
358 if (strncmp(bsdtar->getopt_word, popt->name, optlength) == 0) {
359 match2 = match; /* Record up to two matches. */
360 match = popt;
361 /* If it's an exact match, we're done. */
362 if (strlen(popt->name) == optlength) {
363 match2 = NULL; /* Forget the others. */
364 break;
365 }
366 }
367 }
368
369 /* Fail if there wasn't a unique match. */
370 if (match == NULL) {
371 lafe_warnc(0,
372 "Option %s%s is not supported",
373 long_prefix, bsdtar->getopt_word);
374 return ('?');
375 }
376 if (match2 != NULL) {
377 lafe_warnc(0,
378 "Ambiguous option %s%s (matches --%s and --%s)",
379 long_prefix, bsdtar->getopt_word, match->name, match2->name);
380 return ('?');
381 }
382
383 /* We've found a unique match; does it need an argument? */
384 if (match->required) {
385 /* Argument required: get next word if necessary. */
386 if (bsdtar->argument == NULL) {
387 bsdtar->argument = *bsdtar->argv;
388 if (bsdtar->argument == NULL) {
389 lafe_warnc(0,
390 "Option %s%s requires an argument",
391 long_prefix, match->name);
392 return ('?');
393 }
394 ++bsdtar->argv;
395 --bsdtar->argc;
396 }
397 } else {
398 /* Argument forbidden: fail if there is one. */
399 if (bsdtar->argument != NULL) {
400 lafe_warnc(0,
401 "Option %s%s does not allow an argument",
402 long_prefix, match->name);
403 return ('?');
404 }
405 }
406 return (match->equivalent);
407 }
408
409 return (opt);
410 }
411