bsdtar.c revision 1.1 1 1.1 joerg /*-
2 1.1 joerg * Copyright (c) 2003-2007 Tim Kientzle
3 1.1 joerg * All rights reserved.
4 1.1 joerg *
5 1.1 joerg * Redistribution and use in source and binary forms, with or without
6 1.1 joerg * modification, are permitted provided that the following conditions
7 1.1 joerg * are met:
8 1.1 joerg * 1. Redistributions of source code must retain the above copyright
9 1.1 joerg * notice, this list of conditions and the following disclaimer.
10 1.1 joerg * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 joerg * notice, this list of conditions and the following disclaimer in the
12 1.1 joerg * documentation and/or other materials provided with the distribution.
13 1.1 joerg *
14 1.1 joerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 1.1 joerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 1.1 joerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 1.1 joerg * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 1.1 joerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 1.1 joerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 1.1 joerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 1.1 joerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 1.1 joerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 1.1 joerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 1.1 joerg */
25 1.1 joerg
26 1.1 joerg #include "bsdtar_platform.h"
27 1.1 joerg __FBSDID("$FreeBSD: src/usr.bin/tar/bsdtar.c,v 1.91 2008/05/26 17:10:10 kientzle Exp $");
28 1.1 joerg
29 1.1 joerg #ifdef HAVE_SYS_PARAM_H
30 1.1 joerg #include <sys/param.h>
31 1.1 joerg #endif
32 1.1 joerg #ifdef HAVE_SYS_STAT_H
33 1.1 joerg #include <sys/stat.h>
34 1.1 joerg #endif
35 1.1 joerg #ifdef HAVE_ERRNO_H
36 1.1 joerg #include <errno.h>
37 1.1 joerg #endif
38 1.1 joerg #ifdef HAVE_FCNTL_H
39 1.1 joerg #include <fcntl.h>
40 1.1 joerg #endif
41 1.1 joerg #ifdef HAVE_GETOPT_LONG
42 1.1 joerg #include <getopt.h>
43 1.1 joerg #else
44 1.1 joerg struct option {
45 1.1 joerg const char *name;
46 1.1 joerg int has_arg;
47 1.1 joerg int *flag;
48 1.1 joerg int val;
49 1.1 joerg };
50 1.1 joerg #define no_argument 0
51 1.1 joerg #define required_argument 1
52 1.1 joerg #endif
53 1.1 joerg #ifdef HAVE_LANGINFO_H
54 1.1 joerg #include <langinfo.h>
55 1.1 joerg #endif
56 1.1 joerg #ifdef HAVE_LOCALE_H
57 1.1 joerg #include <locale.h>
58 1.1 joerg #endif
59 1.1 joerg #ifdef HAVE_PATHS_H
60 1.1 joerg #include <paths.h>
61 1.1 joerg #endif
62 1.1 joerg #include <stdio.h>
63 1.1 joerg #ifdef HAVE_STDLIB_H
64 1.1 joerg #include <stdlib.h>
65 1.1 joerg #endif
66 1.1 joerg #ifdef HAVE_STRING_H
67 1.1 joerg #include <string.h>
68 1.1 joerg #endif
69 1.1 joerg #ifdef HAVE_TIME_H
70 1.1 joerg #include <time.h>
71 1.1 joerg #endif
72 1.1 joerg #ifdef HAVE_UNISTD_H
73 1.1 joerg #include <unistd.h>
74 1.1 joerg #endif
75 1.1 joerg #if HAVE_ZLIB_H
76 1.1 joerg #include <zlib.h>
77 1.1 joerg #endif
78 1.1 joerg
79 1.1 joerg #include "bsdtar.h"
80 1.1 joerg
81 1.1 joerg #if !HAVE_DECL_OPTARG
82 1.1 joerg extern int optarg;
83 1.1 joerg #endif
84 1.1 joerg
85 1.1 joerg #if !HAVE_DECL_OPTIND
86 1.1 joerg extern int optind;
87 1.1 joerg #endif
88 1.1 joerg
89 1.1 joerg /*
90 1.1 joerg * Per POSIX.1-1988, tar defaults to reading/writing archives to/from
91 1.1 joerg * the default tape device for the system. Pick something reasonable here.
92 1.1 joerg */
93 1.1 joerg #ifdef __linux
94 1.1 joerg #define _PATH_DEFTAPE "/dev/st0"
95 1.1 joerg #endif
96 1.1 joerg
97 1.1 joerg #ifndef _PATH_DEFTAPE
98 1.1 joerg #define _PATH_DEFTAPE "/dev/tape"
99 1.1 joerg #endif
100 1.1 joerg
101 1.1 joerg /* External function to parse a date/time string (from getdate.y) */
102 1.1 joerg time_t get_date(const char *);
103 1.1 joerg
104 1.1 joerg static int bsdtar_getopt(struct bsdtar *, const char *optstring,
105 1.1 joerg const struct option **poption);
106 1.1 joerg static void long_help(struct bsdtar *);
107 1.1 joerg static void only_mode(struct bsdtar *, const char *opt,
108 1.1 joerg const char *valid);
109 1.1 joerg static char ** rewrite_argv(struct bsdtar *,
110 1.1 joerg int *argc, char ** src_argv,
111 1.1 joerg const char *optstring);
112 1.1 joerg static void set_mode(struct bsdtar *, char opt);
113 1.1 joerg static void version(void);
114 1.1 joerg
115 1.1 joerg /*
116 1.1 joerg * The leading '+' here forces the GNU version of getopt() (as well as
117 1.1 joerg * both the GNU and BSD versions of getopt_long) to stop at the first
118 1.1 joerg * non-option. Otherwise, GNU getopt() permutes the arguments and
119 1.1 joerg * screws up -C processing.
120 1.1 joerg */
121 1.1 joerg static const char *tar_opts = "+Bb:C:cf:HhI:jkLlmnOoPprts:ST:UuvW:wX:xyZz";
122 1.1 joerg
123 1.1 joerg /*
124 1.1 joerg * Most of these long options are deliberately not documented. They
125 1.1 joerg * are provided only to make life easier for people who also use GNU tar.
126 1.1 joerg * The only long options documented in the manual page are the ones
127 1.1 joerg * with no corresponding short option, such as --exclude, --nodump,
128 1.1 joerg * and --fast-read.
129 1.1 joerg *
130 1.1 joerg * On systems that lack getopt_long, long options can be specified
131 1.1 joerg * using -W longopt and -W longopt=value, e.g. "-W nodump" is the same
132 1.1 joerg * as "--nodump" and "-W exclude=pattern" is the same as "--exclude
133 1.1 joerg * pattern". This does not rely the GNU getopt() "W;" extension, so
134 1.1 joerg * should work correctly on any system with a POSIX-compliant getopt().
135 1.1 joerg */
136 1.1 joerg
137 1.1 joerg /* Fake short equivalents for long options that otherwise lack them. */
138 1.1 joerg enum {
139 1.1 joerg OPTION_CHECK_LINKS = 1,
140 1.1 joerg OPTION_CHROOT,
141 1.1 joerg OPTION_EXCLUDE,
142 1.1 joerg OPTION_FORMAT,
143 1.1 joerg OPTION_HELP,
144 1.1 joerg OPTION_INCLUDE,
145 1.1 joerg OPTION_KEEP_NEWER_FILES,
146 1.1 joerg OPTION_NEWER_CTIME,
147 1.1 joerg OPTION_NEWER_CTIME_THAN,
148 1.1 joerg OPTION_NEWER_MTIME,
149 1.1 joerg OPTION_NEWER_MTIME_THAN,
150 1.1 joerg OPTION_NODUMP,
151 1.1 joerg OPTION_NO_SAME_OWNER,
152 1.1 joerg OPTION_NO_SAME_PERMISSIONS,
153 1.1 joerg OPTION_NULL,
154 1.1 joerg OPTION_NUMERIC_OWNER,
155 1.1 joerg OPTION_ONE_FILE_SYSTEM,
156 1.1 joerg OPTION_POSIX,
157 1.1 joerg OPTION_STRIP_COMPONENTS,
158 1.1 joerg OPTION_TOTALS,
159 1.1 joerg OPTION_USE_COMPRESS_PROGRAM,
160 1.1 joerg OPTION_VERSION
161 1.1 joerg };
162 1.1 joerg
163 1.1 joerg /*
164 1.1 joerg * If you add anything, be very careful to keep this list properly
165 1.1 joerg * sorted, as the -W logic relies on it.
166 1.1 joerg */
167 1.1 joerg static const struct option tar_longopts[] = {
168 1.1 joerg { "absolute-paths", no_argument, NULL, 'P' },
169 1.1 joerg { "append", no_argument, NULL, 'r' },
170 1.1 joerg { "block-size", required_argument, NULL, 'b' },
171 1.1 joerg { "bunzip2", no_argument, NULL, 'j' },
172 1.1 joerg { "bzip", no_argument, NULL, 'j' },
173 1.1 joerg { "bzip2", no_argument, NULL, 'j' },
174 1.1 joerg { "cd", required_argument, NULL, 'C' },
175 1.1 joerg { "check-links", no_argument, NULL, OPTION_CHECK_LINKS },
176 1.1 joerg { "chroot", no_argument, NULL, OPTION_CHROOT },
177 1.1 joerg { "compress", no_argument, NULL, 'Z' },
178 1.1 joerg { "confirmation", no_argument, NULL, 'w' },
179 1.1 joerg { "create", no_argument, NULL, 'c' },
180 1.1 joerg { "dereference", no_argument, NULL, 'L' },
181 1.1 joerg { "directory", required_argument, NULL, 'C' },
182 1.1 joerg { "exclude", required_argument, NULL, OPTION_EXCLUDE },
183 1.1 joerg { "exclude-from", required_argument, NULL, 'X' },
184 1.1 joerg { "extract", no_argument, NULL, 'x' },
185 1.1 joerg { "fast-read", no_argument, NULL, 'q' },
186 1.1 joerg { "file", required_argument, NULL, 'f' },
187 1.1 joerg { "files-from", required_argument, NULL, 'T' },
188 1.1 joerg { "format", required_argument, NULL, OPTION_FORMAT },
189 1.1 joerg { "gunzip", no_argument, NULL, 'z' },
190 1.1 joerg { "gzip", no_argument, NULL, 'z' },
191 1.1 joerg { "help", no_argument, NULL, OPTION_HELP },
192 1.1 joerg { "include", required_argument, NULL, OPTION_INCLUDE },
193 1.1 joerg { "interactive", no_argument, NULL, 'w' },
194 1.1 joerg { "insecure", no_argument, NULL, 'P' },
195 1.1 joerg { "keep-newer-files", no_argument, NULL, OPTION_KEEP_NEWER_FILES },
196 1.1 joerg { "keep-old-files", no_argument, NULL, 'k' },
197 1.1 joerg { "list", no_argument, NULL, 't' },
198 1.1 joerg { "modification-time", no_argument, NULL, 'm' },
199 1.1 joerg { "newer", required_argument, NULL, OPTION_NEWER_CTIME },
200 1.1 joerg { "newer-ctime", required_argument, NULL, OPTION_NEWER_CTIME },
201 1.1 joerg { "newer-ctime-than", required_argument, NULL, OPTION_NEWER_CTIME_THAN },
202 1.1 joerg { "newer-mtime", required_argument, NULL, OPTION_NEWER_MTIME },
203 1.1 joerg { "newer-mtime-than", required_argument, NULL, OPTION_NEWER_MTIME_THAN },
204 1.1 joerg { "newer-than", required_argument, NULL, OPTION_NEWER_CTIME_THAN },
205 1.1 joerg { "nodump", no_argument, NULL, OPTION_NODUMP },
206 1.1 joerg { "norecurse", no_argument, NULL, 'n' },
207 1.1 joerg { "no-recursion", no_argument, NULL, 'n' },
208 1.1 joerg { "no-same-owner", no_argument, NULL, OPTION_NO_SAME_OWNER },
209 1.1 joerg { "no-same-permissions",no_argument, NULL, OPTION_NO_SAME_PERMISSIONS },
210 1.1 joerg { "null", no_argument, NULL, OPTION_NULL },
211 1.1 joerg { "numeric-owner", no_argument, NULL, OPTION_NUMERIC_OWNER },
212 1.1 joerg { "one-file-system", no_argument, NULL, OPTION_ONE_FILE_SYSTEM },
213 1.1 joerg { "posix", no_argument, NULL, OPTION_POSIX },
214 1.1 joerg { "preserve-permissions", no_argument, NULL, 'p' },
215 1.1 joerg { "read-full-blocks", no_argument, NULL, 'B' },
216 1.1 joerg { "same-permissions", no_argument, NULL, 'p' },
217 1.1 joerg { "strip-components", required_argument, NULL, OPTION_STRIP_COMPONENTS },
218 1.1 joerg { "to-stdout", no_argument, NULL, 'O' },
219 1.1 joerg { "totals", no_argument, NULL, OPTION_TOTALS },
220 1.1 joerg { "uncompress", no_argument, NULL, 'Z' },
221 1.1 joerg { "unlink", no_argument, NULL, 'U' },
222 1.1 joerg { "unlink-first", no_argument, NULL, 'U' },
223 1.1 joerg { "update", no_argument, NULL, 'u' },
224 1.1 joerg { "use-compress-program",
225 1.1 joerg required_argument, NULL, OPTION_USE_COMPRESS_PROGRAM },
226 1.1 joerg { "verbose", no_argument, NULL, 'v' },
227 1.1 joerg { "version", no_argument, NULL, OPTION_VERSION },
228 1.1 joerg { NULL, 0, NULL, 0 }
229 1.1 joerg };
230 1.1 joerg
231 1.1 joerg /* A basic set of security flags to request from libarchive. */
232 1.1 joerg #define SECURITY \
233 1.1 joerg (ARCHIVE_EXTRACT_SECURE_SYMLINKS \
234 1.1 joerg | ARCHIVE_EXTRACT_SECURE_NODOTDOT)
235 1.1 joerg
236 1.1 joerg int
237 1.1 joerg main(int argc, char **argv)
238 1.1 joerg {
239 1.1 joerg struct bsdtar *bsdtar, bsdtar_storage;
240 1.1 joerg const struct option *option;
241 1.1 joerg int opt, t;
242 1.1 joerg char option_o;
243 1.1 joerg char possible_help_request;
244 1.1 joerg char buff[16];
245 1.1 joerg
246 1.1 joerg /*
247 1.1 joerg * Use a pointer for consistency, but stack-allocated storage
248 1.1 joerg * for ease of cleanup.
249 1.1 joerg */
250 1.1 joerg bsdtar = &bsdtar_storage;
251 1.1 joerg memset(bsdtar, 0, sizeof(*bsdtar));
252 1.1 joerg bsdtar->fd = -1; /* Mark as "unused" */
253 1.1 joerg option_o = 0;
254 1.1 joerg
255 1.1 joerg /* Need bsdtar->progname before calling bsdtar_warnc. */
256 1.1 joerg if (*argv == NULL)
257 1.1 joerg bsdtar->progname = "bsdtar";
258 1.1 joerg else {
259 1.1 joerg bsdtar->progname = strrchr(*argv, '/');
260 1.1 joerg if (bsdtar->progname != NULL)
261 1.1 joerg bsdtar->progname++;
262 1.1 joerg else
263 1.1 joerg bsdtar->progname = *argv;
264 1.1 joerg }
265 1.1 joerg
266 1.1 joerg if (setlocale(LC_ALL, "") == NULL)
267 1.1 joerg bsdtar_warnc(bsdtar, 0, "Failed to set default locale");
268 1.1 joerg #if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER)
269 1.1 joerg bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd');
270 1.1 joerg #endif
271 1.1 joerg possible_help_request = 0;
272 1.1 joerg
273 1.1 joerg /* Look up uid of current user for future reference */
274 1.1 joerg bsdtar->user_uid = geteuid();
275 1.1 joerg
276 1.1 joerg /* Default: open tape drive. */
277 1.1 joerg bsdtar->filename = getenv("TAPE");
278 1.1 joerg if (bsdtar->filename == NULL)
279 1.1 joerg bsdtar->filename = _PATH_DEFTAPE;
280 1.1 joerg
281 1.1 joerg /* Default: preserve mod time on extract */
282 1.1 joerg bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME;
283 1.1 joerg
284 1.1 joerg /* Default: Perform basic security checks. */
285 1.1 joerg bsdtar->extract_flags |= SECURITY;
286 1.1 joerg
287 1.1 joerg /* Defaults for root user: */
288 1.1 joerg if (bsdtar->user_uid == 0) {
289 1.1 joerg /* --same-owner */
290 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER;
291 1.1 joerg /* -p */
292 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
293 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
294 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
295 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
296 1.1 joerg }
297 1.1 joerg
298 1.1 joerg /* Rewrite traditional-style tar arguments, if used. */
299 1.1 joerg argv = rewrite_argv(bsdtar, &argc, argv, tar_opts);
300 1.1 joerg
301 1.1 joerg bsdtar->argv = argv;
302 1.1 joerg bsdtar->argc = argc;
303 1.1 joerg
304 1.1 joerg /* Process all remaining arguments now. */
305 1.1 joerg /*
306 1.1 joerg * Comments following each option indicate where that option
307 1.1 joerg * originated: SUSv2, POSIX, GNU tar, star, etc. If there's
308 1.1 joerg * no such comment, then I don't know of anyone else who
309 1.1 joerg * implements that option.
310 1.1 joerg */
311 1.1 joerg while ((opt = bsdtar_getopt(bsdtar, tar_opts, &option)) != -1) {
312 1.1 joerg switch (opt) {
313 1.1 joerg case 'B': /* GNU tar */
314 1.1 joerg /* libarchive doesn't need this; just ignore it. */
315 1.1 joerg break;
316 1.1 joerg case 'b': /* SUSv2 */
317 1.1 joerg t = atoi(optarg);
318 1.1 joerg if (t <= 0 || t > 1024)
319 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
320 1.1 joerg "Argument to -b is out of range (1..1024)");
321 1.1 joerg bsdtar->bytes_per_block = 512 * t;
322 1.1 joerg break;
323 1.1 joerg case 'C': /* GNU tar */
324 1.1 joerg set_chdir(bsdtar, optarg);
325 1.1 joerg break;
326 1.1 joerg case 'c': /* SUSv2 */
327 1.1 joerg set_mode(bsdtar, opt);
328 1.1 joerg break;
329 1.1 joerg case OPTION_CHECK_LINKS: /* GNU tar */
330 1.1 joerg bsdtar->option_warn_links = 1;
331 1.1 joerg break;
332 1.1 joerg case OPTION_CHROOT: /* NetBSD */
333 1.1 joerg bsdtar->option_chroot = 1;
334 1.1 joerg break;
335 1.1 joerg case OPTION_EXCLUDE: /* GNU tar */
336 1.1 joerg if (exclude(bsdtar, optarg))
337 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
338 1.1 joerg "Couldn't exclude %s\n", optarg);
339 1.1 joerg break;
340 1.1 joerg case OPTION_FORMAT: /* GNU tar, others */
341 1.1 joerg bsdtar->create_format = optarg;
342 1.1 joerg break;
343 1.1 joerg case 'f': /* SUSv2 */
344 1.1 joerg bsdtar->filename = optarg;
345 1.1 joerg if (strcmp(bsdtar->filename, "-") == 0)
346 1.1 joerg bsdtar->filename = NULL;
347 1.1 joerg break;
348 1.1 joerg case 'H': /* BSD convention */
349 1.1 joerg bsdtar->symlink_mode = 'H';
350 1.1 joerg break;
351 1.1 joerg case 'h': /* Linux Standards Base, gtar; synonym for -L */
352 1.1 joerg bsdtar->symlink_mode = 'L';
353 1.1 joerg /* Hack: -h by itself is the "help" command. */
354 1.1 joerg possible_help_request = 1;
355 1.1 joerg break;
356 1.1 joerg case OPTION_HELP: /* GNU tar, others */
357 1.1 joerg long_help(bsdtar);
358 1.1 joerg exit(0);
359 1.1 joerg break;
360 1.1 joerg case 'I': /* GNU tar */
361 1.1 joerg /*
362 1.1 joerg * TODO: Allow 'names' to come from an archive,
363 1.1 joerg * not just a text file. Design a good UI for
364 1.1 joerg * allowing names and mode/owner to be read
365 1.1 joerg * from an archive, with contents coming from
366 1.1 joerg * disk. This can be used to "refresh" an
367 1.1 joerg * archive or to design archives with special
368 1.1 joerg * permissions without having to create those
369 1.1 joerg * permissions on disk.
370 1.1 joerg */
371 1.1 joerg bsdtar->names_from_file = optarg;
372 1.1 joerg break;
373 1.1 joerg case OPTION_INCLUDE:
374 1.1 joerg /*
375 1.1 joerg * Noone else has the @archive extension, so
376 1.1 joerg * noone else needs this to filter entries
377 1.1 joerg * when transforming archives.
378 1.1 joerg */
379 1.1 joerg if (include(bsdtar, optarg))
380 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
381 1.1 joerg "Failed to add %s to inclusion list",
382 1.1 joerg optarg);
383 1.1 joerg break;
384 1.1 joerg case 'j': /* GNU tar */
385 1.1 joerg #if HAVE_LIBBZ2
386 1.1 joerg if (bsdtar->create_compression != '\0')
387 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
388 1.1 joerg "Can't specify both -%c and -%c", opt,
389 1.1 joerg bsdtar->create_compression);
390 1.1 joerg bsdtar->create_compression = opt;
391 1.1 joerg #else
392 1.1 joerg bsdtar_warnc(bsdtar, 0, "-j compression not supported by this version of bsdtar");
393 1.1 joerg usage(bsdtar);
394 1.1 joerg #endif
395 1.1 joerg break;
396 1.1 joerg case 'k': /* GNU tar */
397 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE;
398 1.1 joerg break;
399 1.1 joerg case OPTION_KEEP_NEWER_FILES: /* GNU tar */
400 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
401 1.1 joerg break;
402 1.1 joerg case 'L': /* BSD convention */
403 1.1 joerg bsdtar->symlink_mode = 'L';
404 1.1 joerg break;
405 1.1 joerg case 'l': /* SUSv2 and GNU tar beginning with 1.16 */
406 1.1 joerg /* GNU tar 1.13 used -l for --one-file-system */
407 1.1 joerg bsdtar->option_warn_links = 1;
408 1.1 joerg break;
409 1.1 joerg case 'm': /* SUSv2 */
410 1.1 joerg bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME;
411 1.1 joerg break;
412 1.1 joerg case 'n': /* GNU tar */
413 1.1 joerg bsdtar->option_no_subdirs = 1;
414 1.1 joerg break;
415 1.1 joerg /*
416 1.1 joerg * Selecting files by time:
417 1.1 joerg * --newer-?time='date' Only files newer than 'date'
418 1.1 joerg * --newer-?time-than='file' Only files newer than time
419 1.1 joerg * on specified file (useful for incremental backups)
420 1.1 joerg * TODO: Add corresponding "older" options to reverse these.
421 1.1 joerg */
422 1.1 joerg case OPTION_NEWER_CTIME: /* GNU tar */
423 1.1 joerg bsdtar->newer_ctime_sec = get_date(optarg);
424 1.1 joerg break;
425 1.1 joerg case OPTION_NEWER_CTIME_THAN:
426 1.1 joerg {
427 1.1 joerg struct stat st;
428 1.1 joerg if (stat(optarg, &st) != 0)
429 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
430 1.1 joerg "Can't open file %s", optarg);
431 1.1 joerg bsdtar->newer_ctime_sec = st.st_ctime;
432 1.1 joerg bsdtar->newer_ctime_nsec =
433 1.1 joerg ARCHIVE_STAT_CTIME_NANOS(&st);
434 1.1 joerg }
435 1.1 joerg break;
436 1.1 joerg case OPTION_NEWER_MTIME: /* GNU tar */
437 1.1 joerg bsdtar->newer_mtime_sec = get_date(optarg);
438 1.1 joerg break;
439 1.1 joerg case OPTION_NEWER_MTIME_THAN:
440 1.1 joerg {
441 1.1 joerg struct stat st;
442 1.1 joerg if (stat(optarg, &st) != 0)
443 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
444 1.1 joerg "Can't open file %s", optarg);
445 1.1 joerg bsdtar->newer_mtime_sec = st.st_mtime;
446 1.1 joerg bsdtar->newer_mtime_nsec =
447 1.1 joerg ARCHIVE_STAT_MTIME_NANOS(&st);
448 1.1 joerg }
449 1.1 joerg break;
450 1.1 joerg case OPTION_NODUMP: /* star */
451 1.1 joerg bsdtar->option_honor_nodump = 1;
452 1.1 joerg break;
453 1.1 joerg case OPTION_NO_SAME_OWNER: /* GNU tar */
454 1.1 joerg bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
455 1.1 joerg break;
456 1.1 joerg case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */
457 1.1 joerg bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_PERM;
458 1.1 joerg bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_ACL;
459 1.1 joerg bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_XATTR;
460 1.1 joerg bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_FFLAGS;
461 1.1 joerg break;
462 1.1 joerg case OPTION_NULL: /* GNU tar */
463 1.1 joerg bsdtar->option_null++;
464 1.1 joerg break;
465 1.1 joerg case OPTION_NUMERIC_OWNER: /* GNU tar */
466 1.1 joerg bsdtar->option_numeric_owner++;
467 1.1 joerg break;
468 1.1 joerg case 'O': /* GNU tar */
469 1.1 joerg bsdtar->option_stdout = 1;
470 1.1 joerg break;
471 1.1 joerg case 'o': /* SUSv2 and GNU conflict here, but not fatally */
472 1.1 joerg option_o = 1; /* Record it and resolve it later. */
473 1.1 joerg break;
474 1.1 joerg case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
475 1.1 joerg bsdtar->option_dont_traverse_mounts = 1;
476 1.1 joerg break;
477 1.1 joerg #if 0
478 1.1 joerg /*
479 1.1 joerg * The common BSD -P option is not necessary, since
480 1.1 joerg * our default is to archive symlinks, not follow
481 1.1 joerg * them. This is convenient, as -P conflicts with GNU
482 1.1 joerg * tar anyway.
483 1.1 joerg */
484 1.1 joerg case 'P': /* BSD convention */
485 1.1 joerg /* Default behavior, no option necessary. */
486 1.1 joerg break;
487 1.1 joerg #endif
488 1.1 joerg case 'P': /* GNU tar */
489 1.1 joerg bsdtar->extract_flags &= ~SECURITY;
490 1.1 joerg bsdtar->option_absolute_paths = 1;
491 1.1 joerg break;
492 1.1 joerg case 'p': /* GNU tar, star */
493 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM;
494 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL;
495 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_XATTR;
496 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
497 1.1 joerg break;
498 1.1 joerg case OPTION_POSIX: /* GNU tar */
499 1.1 joerg bsdtar->create_format = "pax";
500 1.1 joerg break;
501 1.1 joerg case 'q': /* FreeBSD GNU tar --fast-read, NetBSD -q */
502 1.1 joerg bsdtar->option_fast_read = 1;
503 1.1 joerg break;
504 1.1 joerg case 'r': /* SUSv2 */
505 1.1 joerg set_mode(bsdtar, opt);
506 1.1 joerg break;
507 1.1 joerg case 'S': /* NetBSD pax-as-tar */
508 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_SPARSE;
509 1.1 joerg break;
510 1.1 joerg case 's': /* NetBSD pax-as-tar */
511 1.1 joerg #if HAVE_REGEX_H
512 1.1 joerg add_substitution(bsdtar, optarg);
513 1.1 joerg #else
514 1.1 joerg bsdtar_warnc(bsdtar, 0, "-s is not supported by this version of bsdtar");
515 1.1 joerg usage(bsdtar);
516 1.1 joerg #endif
517 1.1 joerg break;
518 1.1 joerg case OPTION_STRIP_COMPONENTS: /* GNU tar 1.15 */
519 1.1 joerg bsdtar->strip_components = atoi(optarg);
520 1.1 joerg break;
521 1.1 joerg case 'T': /* GNU tar */
522 1.1 joerg bsdtar->names_from_file = optarg;
523 1.1 joerg break;
524 1.1 joerg case 't': /* SUSv2 */
525 1.1 joerg set_mode(bsdtar, opt);
526 1.1 joerg bsdtar->verbose++;
527 1.1 joerg break;
528 1.1 joerg case OPTION_TOTALS: /* GNU tar */
529 1.1 joerg bsdtar->option_totals++;
530 1.1 joerg break;
531 1.1 joerg case 'U': /* GNU tar */
532 1.1 joerg bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK;
533 1.1 joerg bsdtar->option_unlink_first = 1;
534 1.1 joerg break;
535 1.1 joerg case 'u': /* SUSv2 */
536 1.1 joerg set_mode(bsdtar, opt);
537 1.1 joerg break;
538 1.1 joerg case 'v': /* SUSv2 */
539 1.1 joerg bsdtar->verbose++;
540 1.1 joerg break;
541 1.1 joerg case OPTION_VERSION: /* GNU convention */
542 1.1 joerg version();
543 1.1 joerg break;
544 1.1 joerg #if 0
545 1.1 joerg /*
546 1.1 joerg * The -W longopt feature is handled inside of
547 1.1 joerg * bsdtar_getop(), so -W is not available here.
548 1.1 joerg */
549 1.1 joerg case 'W': /* Obscure, but useful GNU convention. */
550 1.1 joerg break;
551 1.1 joerg #endif
552 1.1 joerg case 'w': /* SUSv2 */
553 1.1 joerg bsdtar->option_interactive = 1;
554 1.1 joerg break;
555 1.1 joerg case 'X': /* GNU tar */
556 1.1 joerg if (exclude_from_file(bsdtar, optarg))
557 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
558 1.1 joerg "failed to process exclusions from file %s",
559 1.1 joerg optarg);
560 1.1 joerg break;
561 1.1 joerg case 'x': /* SUSv2 */
562 1.1 joerg set_mode(bsdtar, opt);
563 1.1 joerg break;
564 1.1 joerg case 'y': /* FreeBSD version of GNU tar */
565 1.1 joerg #if HAVE_LIBBZ2
566 1.1 joerg if (bsdtar->create_compression != '\0')
567 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
568 1.1 joerg "Can't specify both -%c and -%c", opt,
569 1.1 joerg bsdtar->create_compression);
570 1.1 joerg bsdtar->create_compression = opt;
571 1.1 joerg #else
572 1.1 joerg bsdtar_warnc(bsdtar, 0, "-y compression not supported by this version of bsdtar");
573 1.1 joerg usage(bsdtar);
574 1.1 joerg #endif
575 1.1 joerg break;
576 1.1 joerg case 'Z': /* GNU tar */
577 1.1 joerg if (bsdtar->create_compression != '\0')
578 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
579 1.1 joerg "Can't specify both -%c and -%c", opt,
580 1.1 joerg bsdtar->create_compression);
581 1.1 joerg bsdtar->create_compression = opt;
582 1.1 joerg break;
583 1.1 joerg case 'z': /* GNU tar, star, many others */
584 1.1 joerg #if HAVE_LIBZ
585 1.1 joerg if (bsdtar->create_compression != '\0')
586 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
587 1.1 joerg "Can't specify both -%c and -%c", opt,
588 1.1 joerg bsdtar->create_compression);
589 1.1 joerg bsdtar->create_compression = opt;
590 1.1 joerg #else
591 1.1 joerg bsdtar_warnc(bsdtar, 0, "-z compression not supported by this version of bsdtar");
592 1.1 joerg usage(bsdtar);
593 1.1 joerg #endif
594 1.1 joerg break;
595 1.1 joerg case OPTION_USE_COMPRESS_PROGRAM:
596 1.1 joerg bsdtar->compress_program = optarg;
597 1.1 joerg break;
598 1.1 joerg default:
599 1.1 joerg usage(bsdtar);
600 1.1 joerg }
601 1.1 joerg }
602 1.1 joerg
603 1.1 joerg /*
604 1.1 joerg * Sanity-check options.
605 1.1 joerg */
606 1.1 joerg
607 1.1 joerg /* If no "real" mode was specified, treat -h as --help. */
608 1.1 joerg if ((bsdtar->mode == '\0') && possible_help_request) {
609 1.1 joerg long_help(bsdtar);
610 1.1 joerg exit(0);
611 1.1 joerg }
612 1.1 joerg
613 1.1 joerg /* Otherwise, a mode is required. */
614 1.1 joerg if (bsdtar->mode == '\0')
615 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
616 1.1 joerg "Must specify one of -c, -r, -t, -u, -x");
617 1.1 joerg
618 1.1 joerg /* Check boolean options only permitted in certain modes. */
619 1.1 joerg if (bsdtar->option_dont_traverse_mounts)
620 1.1 joerg only_mode(bsdtar, "--one-file-system", "cru");
621 1.1 joerg if (bsdtar->option_fast_read)
622 1.1 joerg only_mode(bsdtar, "--fast-read", "xt");
623 1.1 joerg if (bsdtar->option_honor_nodump)
624 1.1 joerg only_mode(bsdtar, "--nodump", "cru");
625 1.1 joerg if (option_o > 0) {
626 1.1 joerg switch (bsdtar->mode) {
627 1.1 joerg case 'c':
628 1.1 joerg /*
629 1.1 joerg * In GNU tar, -o means "old format." The
630 1.1 joerg * "ustar" format is the closest thing
631 1.1 joerg * supported by libarchive.
632 1.1 joerg */
633 1.1 joerg bsdtar->create_format = "ustar";
634 1.1 joerg /* TODO: bsdtar->create_format = "v7"; */
635 1.1 joerg break;
636 1.1 joerg case 'x':
637 1.1 joerg /* POSIX-compatible behavior. */
638 1.1 joerg bsdtar->option_no_owner = 1;
639 1.1 joerg bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
640 1.1 joerg break;
641 1.1 joerg default:
642 1.1 joerg only_mode(bsdtar, "-o", "xc");
643 1.1 joerg break;
644 1.1 joerg }
645 1.1 joerg }
646 1.1 joerg if (bsdtar->option_no_subdirs)
647 1.1 joerg only_mode(bsdtar, "-n", "cru");
648 1.1 joerg if (bsdtar->option_stdout)
649 1.1 joerg only_mode(bsdtar, "-O", "xt");
650 1.1 joerg if (bsdtar->option_unlink_first)
651 1.1 joerg only_mode(bsdtar, "-U", "x");
652 1.1 joerg if (bsdtar->option_warn_links)
653 1.1 joerg only_mode(bsdtar, "--check-links", "cr");
654 1.1 joerg
655 1.1 joerg /* Check other parameters only permitted in certain modes. */
656 1.1 joerg if (bsdtar->create_compression != '\0') {
657 1.1 joerg strcpy(buff, "-?");
658 1.1 joerg buff[1] = bsdtar->create_compression;
659 1.1 joerg only_mode(bsdtar, buff, "cxt");
660 1.1 joerg }
661 1.1 joerg if (bsdtar->create_format != NULL)
662 1.1 joerg only_mode(bsdtar, "--format", "cru");
663 1.1 joerg if (bsdtar->symlink_mode != '\0') {
664 1.1 joerg strcpy(buff, "-?");
665 1.1 joerg buff[1] = bsdtar->symlink_mode;
666 1.1 joerg only_mode(bsdtar, buff, "cru");
667 1.1 joerg }
668 1.1 joerg if (bsdtar->strip_components != 0)
669 1.1 joerg only_mode(bsdtar, "--strip-components", "xt");
670 1.1 joerg
671 1.1 joerg bsdtar->argc -= optind;
672 1.1 joerg bsdtar->argv += optind;
673 1.1 joerg
674 1.1 joerg switch(bsdtar->mode) {
675 1.1 joerg case 'c':
676 1.1 joerg tar_mode_c(bsdtar);
677 1.1 joerg break;
678 1.1 joerg case 'r':
679 1.1 joerg tar_mode_r(bsdtar);
680 1.1 joerg break;
681 1.1 joerg case 't':
682 1.1 joerg tar_mode_t(bsdtar);
683 1.1 joerg break;
684 1.1 joerg case 'u':
685 1.1 joerg tar_mode_u(bsdtar);
686 1.1 joerg break;
687 1.1 joerg case 'x':
688 1.1 joerg tar_mode_x(bsdtar);
689 1.1 joerg break;
690 1.1 joerg }
691 1.1 joerg
692 1.1 joerg cleanup_exclusions(bsdtar);
693 1.1 joerg #if HAVE_REGEX_H
694 1.1 joerg cleanup_substitution(bsdtar);
695 1.1 joerg #endif
696 1.1 joerg
697 1.1 joerg if (bsdtar->return_value != 0)
698 1.1 joerg bsdtar_warnc(bsdtar, 0,
699 1.1 joerg "Error exit delayed from previous errors.");
700 1.1 joerg return (bsdtar->return_value);
701 1.1 joerg }
702 1.1 joerg
703 1.1 joerg static void
704 1.1 joerg set_mode(struct bsdtar *bsdtar, char opt)
705 1.1 joerg {
706 1.1 joerg if (bsdtar->mode != '\0' && bsdtar->mode != opt)
707 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
708 1.1 joerg "Can't specify both -%c and -%c", opt, bsdtar->mode);
709 1.1 joerg bsdtar->mode = opt;
710 1.1 joerg }
711 1.1 joerg
712 1.1 joerg /*
713 1.1 joerg * Verify that the mode is correct.
714 1.1 joerg */
715 1.1 joerg static void
716 1.1 joerg only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes)
717 1.1 joerg {
718 1.1 joerg if (strchr(valid_modes, bsdtar->mode) == NULL)
719 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
720 1.1 joerg "Option %s is not permitted in mode -%c",
721 1.1 joerg opt, bsdtar->mode);
722 1.1 joerg }
723 1.1 joerg
724 1.1 joerg
725 1.1 joerg /*-
726 1.1 joerg * Convert traditional tar arguments into new-style.
727 1.1 joerg * For example,
728 1.1 joerg * tar tvfb file.tar 32 --exclude FOO
729 1.1 joerg * will be converted to
730 1.1 joerg * tar -t -v -f file.tar -b 32 --exclude FOO
731 1.1 joerg *
732 1.1 joerg * This requires building a new argv array. The initial bundled word
733 1.1 joerg * gets expanded into a new string that looks like "-t\0-v\0-f\0-b\0".
734 1.1 joerg * The new argv array has pointers into this string intermingled with
735 1.1 joerg * pointers to the existing arguments. Arguments are moved to
736 1.1 joerg * immediately follow their options.
737 1.1 joerg *
738 1.1 joerg * The optstring argument here is the same one passed to getopt(3).
739 1.1 joerg * It is used to determine which option letters have trailing arguments.
740 1.1 joerg */
741 1.1 joerg char **
742 1.1 joerg rewrite_argv(struct bsdtar *bsdtar, int *argc, char **src_argv,
743 1.1 joerg const char *optstring)
744 1.1 joerg {
745 1.1 joerg char **new_argv, **dest_argv;
746 1.1 joerg const char *p;
747 1.1 joerg char *src, *dest;
748 1.1 joerg
749 1.1 joerg if (src_argv[0] == NULL || src_argv[1] == NULL ||
750 1.1 joerg src_argv[1][0] == '-' || src_argv[1][0] == '\0')
751 1.1 joerg return (src_argv);
752 1.1 joerg
753 1.1 joerg *argc += strlen(src_argv[1]) - 1;
754 1.1 joerg new_argv = malloc((*argc + 1) * sizeof(new_argv[0]));
755 1.1 joerg if (new_argv == NULL)
756 1.1 joerg bsdtar_errc(bsdtar, 1, errno, "No Memory");
757 1.1 joerg
758 1.1 joerg dest_argv = new_argv;
759 1.1 joerg *dest_argv++ = *src_argv++;
760 1.1 joerg
761 1.1 joerg dest = malloc(strlen(*src_argv) * 3);
762 1.1 joerg if (dest == NULL)
763 1.1 joerg bsdtar_errc(bsdtar, 1, errno, "No memory");
764 1.1 joerg for (src = *src_argv++; *src != '\0'; src++) {
765 1.1 joerg *dest_argv++ = dest;
766 1.1 joerg *dest++ = '-';
767 1.1 joerg *dest++ = *src;
768 1.1 joerg *dest++ = '\0';
769 1.1 joerg /* If option takes an argument, insert that into the list. */
770 1.1 joerg for (p = optstring; p != NULL && *p != '\0'; p++) {
771 1.1 joerg if (*p != *src)
772 1.1 joerg continue;
773 1.1 joerg if (p[1] != ':') /* No arg required, done. */
774 1.1 joerg break;
775 1.1 joerg if (*src_argv == NULL) /* No arg available? Error. */
776 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
777 1.1 joerg "Option %c requires an argument",
778 1.1 joerg *src);
779 1.1 joerg *dest_argv++ = *src_argv++;
780 1.1 joerg break;
781 1.1 joerg }
782 1.1 joerg }
783 1.1 joerg
784 1.1 joerg /* Copy remaining arguments, including trailing NULL. */
785 1.1 joerg while ((*dest_argv++ = *src_argv++) != NULL)
786 1.1 joerg ;
787 1.1 joerg
788 1.1 joerg return (new_argv);
789 1.1 joerg }
790 1.1 joerg
791 1.1 joerg void
792 1.1 joerg usage(struct bsdtar *bsdtar)
793 1.1 joerg {
794 1.1 joerg const char *p;
795 1.1 joerg
796 1.1 joerg p = bsdtar->progname;
797 1.1 joerg
798 1.1 joerg fprintf(stderr, "Usage:\n");
799 1.1 joerg fprintf(stderr, " List: %s -tf <archive-filename>\n", p);
800 1.1 joerg fprintf(stderr, " Extract: %s -xf <archive-filename>\n", p);
801 1.1 joerg fprintf(stderr, " Create: %s -cf <archive-filename> [filenames...]\n", p);
802 1.1 joerg #ifdef HAVE_GETOPT_LONG
803 1.1 joerg fprintf(stderr, " Help: %s --help\n", p);
804 1.1 joerg #else
805 1.1 joerg fprintf(stderr, " Help: %s -h\n", p);
806 1.1 joerg #endif
807 1.1 joerg exit(1);
808 1.1 joerg }
809 1.1 joerg
810 1.1 joerg static void
811 1.1 joerg version(void)
812 1.1 joerg {
813 1.1 joerg printf("bsdtar %s - %s\n",
814 1.1 joerg BSDTAR_VERSION_STRING,
815 1.1 joerg archive_version());
816 1.1 joerg exit(0);
817 1.1 joerg }
818 1.1 joerg
819 1.1 joerg static const char *long_help_msg =
820 1.1 joerg "First option must be a mode specifier:\n"
821 1.1 joerg " -c Create -r Add/Replace -t List -u Update -x Extract\n"
822 1.1 joerg "Common Options:\n"
823 1.1 joerg " -b # Use # 512-byte records per I/O block\n"
824 1.1 joerg " -f <filename> Location of archive (default " _PATH_DEFTAPE ")\n"
825 1.1 joerg " -v Verbose\n"
826 1.1 joerg " -w Interactive\n"
827 1.1 joerg "Create: %p -c [options] [<file> | <dir> | @<archive> | -C <dir> ]\n"
828 1.1 joerg " <file>, <dir> add these items to archive\n"
829 1.1 joerg " -z, -j Compress archive with gzip/bzip2\n"
830 1.1 joerg " --format {ustar|pax|cpio|shar} Select archive format\n"
831 1.1 joerg #ifdef HAVE_GETOPT_LONG
832 1.1 joerg " --exclude <pattern> Skip files that match pattern\n"
833 1.1 joerg #else
834 1.1 joerg " -W exclude=<pattern> Skip files that match pattern\n"
835 1.1 joerg #endif
836 1.1 joerg " -C <dir> Change to <dir> before processing remaining files\n"
837 1.1 joerg " @<archive> Add entries from <archive> to output\n"
838 1.1 joerg "List: %p -t [options] [<patterns>]\n"
839 1.1 joerg " <patterns> If specified, list only entries that match\n"
840 1.1 joerg "Extract: %p -x [options] [<patterns>]\n"
841 1.1 joerg " <patterns> If specified, extract only entries that match\n"
842 1.1 joerg " -k Keep (don't overwrite) existing files\n"
843 1.1 joerg " -m Don't restore modification times\n"
844 1.1 joerg " -O Write entries to stdout, don't restore to disk\n"
845 1.1 joerg " -p Restore permissions (including ACLs, owner, file flags)\n";
846 1.1 joerg
847 1.1 joerg
848 1.1 joerg /*
849 1.1 joerg * Note that the word 'bsdtar' will always appear in the first line
850 1.1 joerg * of output.
851 1.1 joerg *
852 1.1 joerg * In particular, /bin/sh scripts that need to test for the presence
853 1.1 joerg * of bsdtar can use the following template:
854 1.1 joerg *
855 1.1 joerg * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \
856 1.1 joerg * echo bsdtar; else echo not bsdtar; fi
857 1.1 joerg */
858 1.1 joerg static void
859 1.1 joerg long_help(struct bsdtar *bsdtar)
860 1.1 joerg {
861 1.1 joerg const char *prog;
862 1.1 joerg const char *p;
863 1.1 joerg
864 1.1 joerg prog = bsdtar->progname;
865 1.1 joerg
866 1.1 joerg fflush(stderr);
867 1.1 joerg
868 1.1 joerg p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : "";
869 1.1 joerg printf("%s%s: manipulate archive files\n", prog, p);
870 1.1 joerg
871 1.1 joerg for (p = long_help_msg; *p != '\0'; p++) {
872 1.1 joerg if (*p == '%') {
873 1.1 joerg if (p[1] == 'p') {
874 1.1 joerg fputs(prog, stdout);
875 1.1 joerg p++;
876 1.1 joerg } else
877 1.1 joerg putchar('%');
878 1.1 joerg } else
879 1.1 joerg putchar(*p);
880 1.1 joerg }
881 1.1 joerg version();
882 1.1 joerg }
883 1.1 joerg
884 1.1 joerg static int
885 1.1 joerg bsdtar_getopt(struct bsdtar *bsdtar, const char *optstring,
886 1.1 joerg const struct option **poption)
887 1.1 joerg {
888 1.1 joerg char *p, *q;
889 1.1 joerg const struct option *option;
890 1.1 joerg int opt;
891 1.1 joerg int option_index;
892 1.1 joerg size_t option_length;
893 1.1 joerg
894 1.1 joerg option_index = -1;
895 1.1 joerg *poption = NULL;
896 1.1 joerg
897 1.1 joerg #ifdef HAVE_GETOPT_LONG
898 1.1 joerg opt = getopt_long(bsdtar->argc, bsdtar->argv, optstring,
899 1.1 joerg tar_longopts, &option_index);
900 1.1 joerg if (option_index > -1)
901 1.1 joerg *poption = tar_longopts + option_index;
902 1.1 joerg #else
903 1.1 joerg opt = getopt(bsdtar->argc, bsdtar->argv, optstring);
904 1.1 joerg #endif
905 1.1 joerg
906 1.1 joerg /* Support long options through -W longopt=value */
907 1.1 joerg if (opt == 'W') {
908 1.1 joerg p = optarg;
909 1.1 joerg q = strchr(optarg, '=');
910 1.1 joerg if (q != NULL) {
911 1.1 joerg option_length = (size_t)(q - p);
912 1.1 joerg optarg = q + 1;
913 1.1 joerg } else {
914 1.1 joerg option_length = strlen(p);
915 1.1 joerg optarg = NULL;
916 1.1 joerg }
917 1.1 joerg option = tar_longopts;
918 1.1 joerg while (option->name != NULL &&
919 1.1 joerg (strlen(option->name) < option_length ||
920 1.1 joerg strncmp(p, option->name, option_length) != 0 )) {
921 1.1 joerg option++;
922 1.1 joerg }
923 1.1 joerg
924 1.1 joerg if (option->name != NULL) {
925 1.1 joerg *poption = option;
926 1.1 joerg opt = option->val;
927 1.1 joerg
928 1.1 joerg /* If the first match was exact, we're done. */
929 1.1 joerg if (strncmp(p, option->name, strlen(option->name)) == 0) {
930 1.1 joerg while (option->name != NULL)
931 1.1 joerg option++;
932 1.1 joerg } else {
933 1.1 joerg /* Check if there's another match. */
934 1.1 joerg option++;
935 1.1 joerg while (option->name != NULL &&
936 1.1 joerg (strlen(option->name) < option_length ||
937 1.1 joerg strncmp(p, option->name, option_length) != 0)) {
938 1.1 joerg option++;
939 1.1 joerg }
940 1.1 joerg }
941 1.1 joerg if (option->name != NULL)
942 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
943 1.1 joerg "Ambiguous option %s "
944 1.1 joerg "(matches both %s and %s)",
945 1.1 joerg p, (*poption)->name, option->name);
946 1.1 joerg
947 1.1 joerg if ((*poption)->has_arg == required_argument
948 1.1 joerg && optarg == NULL)
949 1.1 joerg bsdtar_errc(bsdtar, 1, 0,
950 1.1 joerg "Option \"%s\" requires argument", p);
951 1.1 joerg } else {
952 1.1 joerg opt = '?';
953 1.1 joerg /* TODO: Set up a fake 'struct option' for
954 1.1 joerg * error reporting... ? ? ? */
955 1.1 joerg }
956 1.1 joerg }
957 1.1 joerg
958 1.1 joerg return (opt);
959 1.1 joerg }
960