options.c revision 1.98 1 1.98 christos /* $NetBSD: options.c,v 1.98 2006/10/16 00:11:22 christos Exp $ */
2 1.5 cgd
3 1.1 jtc /*-
4 1.67 agc * Copyright (c) 1992 Keith Muller.
5 1.1 jtc * Copyright (c) 1992, 1993
6 1.1 jtc * The Regents of the University of California. All rights reserved.
7 1.1 jtc *
8 1.1 jtc * This code is derived from software contributed to Berkeley by
9 1.1 jtc * Keith Muller of the University of California, San Diego.
10 1.1 jtc *
11 1.1 jtc * Redistribution and use in source and binary forms, with or without
12 1.1 jtc * modification, are permitted provided that the following conditions
13 1.1 jtc * are met:
14 1.1 jtc * 1. Redistributions of source code must retain the above copyright
15 1.1 jtc * notice, this list of conditions and the following disclaimer.
16 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jtc * notice, this list of conditions and the following disclaimer in the
18 1.1 jtc * documentation and/or other materials provided with the distribution.
19 1.66 agc * 3. Neither the name of the University nor the names of its contributors
20 1.66 agc * may be used to endorse or promote products derived from this software
21 1.66 agc * without specific prior written permission.
22 1.66 agc *
23 1.66 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.66 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.66 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.66 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.66 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.66 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.66 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.66 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.66 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.66 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.66 agc * SUCH DAMAGE.
34 1.66 agc */
35 1.66 agc
36 1.69 lukem #if HAVE_NBTOOL_CONFIG_H
37 1.69 lukem #include "nbtool_config.h"
38 1.69 lukem #endif
39 1.69 lukem
40 1.8 christos #include <sys/cdefs.h>
41 1.69 lukem #if !defined(lint)
42 1.5 cgd #if 0
43 1.5 cgd static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 4/18/94";
44 1.5 cgd #else
45 1.98 christos __RCSID("$NetBSD: options.c,v 1.98 2006/10/16 00:11:22 christos Exp $");
46 1.5 cgd #endif
47 1.1 jtc #endif /* not lint */
48 1.1 jtc
49 1.1 jtc #include <sys/types.h>
50 1.1 jtc #include <sys/time.h>
51 1.1 jtc #include <sys/stat.h>
52 1.1 jtc #include <sys/param.h>
53 1.38 tv #include <ctype.h>
54 1.40 christos #include <errno.h>
55 1.76 jmc #if HAVE_NBTOOL_CONFIG_H
56 1.76 jmc #include "compat_getopt.h"
57 1.76 jmc #else
58 1.38 tv #include <getopt.h>
59 1.76 jmc #endif
60 1.38 tv #include <limits.h>
61 1.1 jtc #include <stdio.h>
62 1.38 tv #include <stdlib.h>
63 1.1 jtc #include <string.h>
64 1.1 jtc #include <unistd.h>
65 1.40 christos #include <paths.h>
66 1.1 jtc #include "pax.h"
67 1.1 jtc #include "options.h"
68 1.1 jtc #include "cpio.h"
69 1.1 jtc #include "tar.h"
70 1.1 jtc #include "extern.h"
71 1.39 lukem #ifndef SMALL
72 1.35 lukem #include "mtree.h"
73 1.39 lukem #endif /* SMALL */
74 1.1 jtc
75 1.1 jtc /*
76 1.1 jtc * Routines which handle command line options
77 1.1 jtc */
78 1.1 jtc
79 1.18 tv static int nopids; /* tar mode: suppress "pids" for -p option */
80 1.74 christos static char flgch[] = FLGCH; /* list of all possible flags (pax) */
81 1.1 jtc static OPLIST *ophead = NULL; /* head for format specific options -x */
82 1.1 jtc static OPLIST *optail = NULL; /* option tail */
83 1.1 jtc
84 1.33 lukem static int no_op(void);
85 1.33 lukem static void printflg(unsigned int);
86 1.33 lukem static int c_frmt(const void *, const void *);
87 1.33 lukem static off_t str_offt(char *);
88 1.40 christos static char *getline(FILE *fp);
89 1.33 lukem static void pax_options(int, char **);
90 1.33 lukem static void pax_usage(void);
91 1.33 lukem static void tar_options(int, char **);
92 1.33 lukem static void tar_usage(void);
93 1.89 christos #ifndef NO_CPIO
94 1.33 lukem static void cpio_options(int, char **);
95 1.33 lukem static void cpio_usage(void);
96 1.89 christos #endif
97 1.1 jtc
98 1.40 christos /* errors from getline */
99 1.40 christos #define GETLINE_FILE_CORRUPT 1
100 1.40 christos #define GETLINE_OUT_OF_MEM 2
101 1.40 christos static int getline_error;
102 1.23 is
103 1.62 wiz #define BZIP2_CMD "bzip2" /* command to run as bzip2 */
104 1.6 mrg #define GZIP_CMD "gzip" /* command to run as gzip */
105 1.6 mrg #define COMPRESS_CMD "compress" /* command to run as compress */
106 1.6 mrg
107 1.1 jtc /*
108 1.47 christos * Long options.
109 1.47 christos */
110 1.47 christos #define OPT_USE_COMPRESS_PROGRAM 0
111 1.47 christos #define OPT_CHECKPOINT 1
112 1.47 christos #define OPT_UNLINK 2
113 1.47 christos #define OPT_HELP 3
114 1.47 christos #define OPT_ATIME_PRESERVE 4
115 1.47 christos #define OPT_IGNORE_FAILED_READ 5
116 1.47 christos #define OPT_REMOVE_FILES 6
117 1.47 christos #define OPT_NULL 7
118 1.47 christos #define OPT_TOTALS 8
119 1.47 christos #define OPT_VERSION 9
120 1.47 christos #define OPT_EXCLUDE 10
121 1.47 christos #define OPT_BLOCK_COMPRESS 11
122 1.47 christos #define OPT_NORECURSE 12
123 1.47 christos #define OPT_FORCE_LOCAL 13
124 1.47 christos #define OPT_INSECURE 14
125 1.48 christos #define OPT_STRICT 15
126 1.78 christos #define OPT_SPARSE 16
127 1.81 jmc #if !HAVE_NBTOOL_CONFIG_H
128 1.80 dsl #define OPT_CHROOT 17
129 1.81 jmc #endif
130 1.47 christos
131 1.47 christos /*
132 1.1 jtc * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
133 1.1 jtc * (see pax.h for description of each function)
134 1.1 jtc *
135 1.26 itohy * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
136 1.1 jtc * read, end_read, st_write, write, end_write, trail,
137 1.77 christos * subtrail, rd_data, wr_data, options
138 1.1 jtc */
139 1.1 jtc
140 1.1 jtc FSUB fsub[] = {
141 1.89 christos #ifndef NO_CPIO
142 1.1 jtc /* 0: OLD BINARY CPIO */
143 1.8 christos { "bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
144 1.8 christos bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, NULL,
145 1.8 christos cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
146 1.1 jtc
147 1.1 jtc /* 1: OLD OCTAL CHARACTER CPIO */
148 1.8 christos { "cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
149 1.8 christos cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, NULL,
150 1.8 christos cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
151 1.1 jtc
152 1.1 jtc /* 2: SVR4 HEX CPIO */
153 1.8 christos { "sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
154 1.8 christos vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, NULL,
155 1.8 christos cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
156 1.1 jtc
157 1.1 jtc /* 3: SVR4 HEX CPIO WITH CRC */
158 1.8 christos { "sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
159 1.8 christos vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, NULL,
160 1.8 christos cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
161 1.89 christos #endif
162 1.1 jtc /* 4: OLD TAR */
163 1.8 christos { "tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
164 1.1 jtc tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
165 1.8 christos NULL, rd_wrfile, wr_rdfile, tar_opt },
166 1.1 jtc
167 1.1 jtc /* 5: POSIX USTAR */
168 1.8 christos { "ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
169 1.1 jtc ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
170 1.8 christos NULL, rd_wrfile, wr_rdfile, bad_opt }
171 1.1 jtc };
172 1.89 christos #ifndef NO_CPIO
173 1.10 mrg #define F_BCPIO 0 /* old binary cpio format */
174 1.10 mrg #define F_CPIO 1 /* old octal character cpio format */
175 1.10 mrg #define F_SV4CPIO 2 /* SVR4 hex cpio format */
176 1.10 mrg #define F_SV4CRC 3 /* SVR4 hex with crc cpio format */
177 1.18 tv #define F_TAR 4 /* old V7 UNIX tar format */
178 1.10 mrg #define F_USTAR 5 /* ustar format */
179 1.89 christos #else
180 1.89 christos #define F_TAR 0 /* old V7 UNIX tar format */
181 1.89 christos #define F_USTAR 1 /* ustar format */
182 1.89 christos #endif
183 1.10 mrg #define DEFLT F_USTAR /* default write format from list above */
184 1.1 jtc
185 1.1 jtc /*
186 1.1 jtc * ford is the archive search order used by get_arc() to determine what kind
187 1.61 wiz * of archive we are dealing with. This helps to properly id archive formats
188 1.1 jtc * some formats may be subsets of others....
189 1.1 jtc */
190 1.89 christos int ford[] = {F_USTAR, F_TAR,
191 1.89 christos #ifndef NO_CPIO
192 1.89 christos F_SV4CRC, F_SV4CPIO, F_CPIO, F_BCPIO,
193 1.89 christos #endif
194 1.89 christos -1};
195 1.1 jtc
196 1.1 jtc /*
197 1.75 christos * filename record separator
198 1.75 christos */
199 1.75 christos int sep = '\n';
200 1.75 christos
201 1.75 christos /*
202 1.97 christos * Do we have -C anywhere?
203 1.97 christos */
204 1.97 christos int havechd = 0;
205 1.97 christos
206 1.97 christos /*
207 1.1 jtc * options()
208 1.1 jtc * figure out if we are pax, tar or cpio. Call the appropriate options
209 1.1 jtc * parser
210 1.1 jtc */
211 1.1 jtc
212 1.1 jtc void
213 1.7 tls options(int argc, char **argv)
214 1.1 jtc {
215 1.1 jtc
216 1.1 jtc /*
217 1.1 jtc * Are we acting like pax, tar or cpio (based on argv[0])
218 1.1 jtc */
219 1.1 jtc if ((argv0 = strrchr(argv[0], '/')) != NULL)
220 1.1 jtc argv0++;
221 1.1 jtc else
222 1.1 jtc argv0 = argv[0];
223 1.1 jtc
224 1.87 christos if (strstr(argv0, NM_TAR)) {
225 1.87 christos argv0 = NM_TAR;
226 1.56 grant tar_options(argc, argv);
227 1.89 christos #ifndef NO_CPIO
228 1.87 christos } else if (strstr(argv0, NM_CPIO)) {
229 1.87 christos argv0 = NM_CPIO;
230 1.56 grant cpio_options(argc, argv);
231 1.89 christos #endif
232 1.87 christos } else {
233 1.13 mycroft argv0 = NM_PAX;
234 1.56 grant pax_options(argc, argv);
235 1.13 mycroft }
236 1.1 jtc }
237 1.1 jtc
238 1.47 christos struct option pax_longopts[] = {
239 1.47 christos { "insecure", no_argument, 0,
240 1.47 christos OPT_INSECURE },
241 1.50 christos { "force-local", no_argument, 0,
242 1.50 christos OPT_FORCE_LOCAL },
243 1.98 christos { 0, 0, 0,
244 1.98 christos 0 },
245 1.47 christos };
246 1.47 christos
247 1.1 jtc /*
248 1.1 jtc * pax_options()
249 1.1 jtc * look at the user specified flags. set globals as required and check if
250 1.1 jtc * the user specified a legal set of flags. If not, complain and exit
251 1.1 jtc */
252 1.1 jtc
253 1.1 jtc static void
254 1.7 tls pax_options(int argc, char **argv)
255 1.1 jtc {
256 1.7 tls int c;
257 1.7 tls int i;
258 1.1 jtc unsigned int flg = 0;
259 1.1 jtc unsigned int bflg = 0;
260 1.7 tls char *pt;
261 1.26 itohy FSUB tmp;
262 1.1 jtc
263 1.1 jtc /*
264 1.1 jtc * process option flags
265 1.1 jtc */
266 1.47 christos while ((c = getopt_long(argc, argv,
267 1.75 christos "0ab:cdf:ijklno:p:rs:tuvwx:zAB:DE:G:HLMN:OPT:U:XYZ",
268 1.47 christos pax_longopts, NULL)) != -1) {
269 1.1 jtc switch (c) {
270 1.75 christos case '0':
271 1.75 christos sep = '\0';
272 1.75 christos break;
273 1.1 jtc case 'a':
274 1.1 jtc /*
275 1.1 jtc * append
276 1.1 jtc */
277 1.1 jtc flg |= AF;
278 1.1 jtc break;
279 1.1 jtc case 'b':
280 1.1 jtc /*
281 1.1 jtc * specify blocksize
282 1.1 jtc */
283 1.1 jtc flg |= BF;
284 1.1 jtc if ((wrblksz = (int)str_offt(optarg)) <= 0) {
285 1.8 christos tty_warn(1, "Invalid block size %s", optarg);
286 1.1 jtc pax_usage();
287 1.1 jtc }
288 1.1 jtc break;
289 1.1 jtc case 'c':
290 1.1 jtc /*
291 1.1 jtc * inverse match on patterns
292 1.1 jtc */
293 1.1 jtc cflag = 1;
294 1.1 jtc flg |= CF;
295 1.1 jtc break;
296 1.1 jtc case 'd':
297 1.1 jtc /*
298 1.1 jtc * match only dir on extract, not the subtree at dir
299 1.1 jtc */
300 1.1 jtc dflag = 1;
301 1.1 jtc flg |= DF;
302 1.1 jtc break;
303 1.1 jtc case 'f':
304 1.1 jtc /*
305 1.1 jtc * filename where the archive is stored
306 1.1 jtc */
307 1.1 jtc arcname = optarg;
308 1.1 jtc flg |= FF;
309 1.1 jtc break;
310 1.1 jtc case 'i':
311 1.1 jtc /*
312 1.1 jtc * interactive file rename
313 1.1 jtc */
314 1.1 jtc iflag = 1;
315 1.1 jtc flg |= IF;
316 1.1 jtc break;
317 1.63 christos case 'j':
318 1.63 christos /*
319 1.63 christos * pass through bzip2
320 1.63 christos */
321 1.63 christos jflag = 1;
322 1.63 christos gzip_program = BZIP2_CMD;
323 1.63 christos break;
324 1.1 jtc case 'k':
325 1.1 jtc /*
326 1.1 jtc * do not clobber files that exist
327 1.1 jtc */
328 1.1 jtc kflag = 1;
329 1.1 jtc flg |= KF;
330 1.1 jtc break;
331 1.1 jtc case 'l':
332 1.1 jtc /*
333 1.1 jtc * try to link src to dest with copy (-rw)
334 1.1 jtc */
335 1.1 jtc lflag = 1;
336 1.1 jtc flg |= LF;
337 1.1 jtc break;
338 1.1 jtc case 'n':
339 1.1 jtc /*
340 1.1 jtc * select first match for a pattern only
341 1.1 jtc */
342 1.1 jtc nflag = 1;
343 1.1 jtc flg |= NF;
344 1.1 jtc break;
345 1.1 jtc case 'o':
346 1.1 jtc /*
347 1.1 jtc * pass format specific options
348 1.1 jtc */
349 1.1 jtc flg |= OF;
350 1.1 jtc if (opt_add(optarg) < 0)
351 1.1 jtc pax_usage();
352 1.1 jtc break;
353 1.1 jtc case 'p':
354 1.1 jtc /*
355 1.1 jtc * specify file characteristic options
356 1.1 jtc */
357 1.1 jtc for (pt = optarg; *pt != '\0'; ++pt) {
358 1.1 jtc switch(*pt) {
359 1.1 jtc case 'a':
360 1.1 jtc /*
361 1.1 jtc * do not preserve access time
362 1.1 jtc */
363 1.1 jtc patime = 0;
364 1.1 jtc break;
365 1.1 jtc case 'e':
366 1.1 jtc /*
367 1.1 jtc * preserve user id, group id, file
368 1.1 jtc * mode, access/modification times
369 1.24 mrg * and file flags.
370 1.1 jtc */
371 1.1 jtc pids = 1;
372 1.1 jtc pmode = 1;
373 1.1 jtc patime = 1;
374 1.1 jtc pmtime = 1;
375 1.24 mrg pfflags = 1;
376 1.24 mrg break;
377 1.25 mycroft #if 0
378 1.24 mrg case 'f':
379 1.24 mrg /*
380 1.24 mrg * do not preserve file flags
381 1.24 mrg */
382 1.24 mrg pfflags = 0;
383 1.1 jtc break;
384 1.25 mycroft #endif
385 1.1 jtc case 'm':
386 1.1 jtc /*
387 1.1 jtc * do not preserve modification time
388 1.1 jtc */
389 1.1 jtc pmtime = 0;
390 1.1 jtc break;
391 1.1 jtc case 'o':
392 1.1 jtc /*
393 1.1 jtc * preserve uid/gid
394 1.1 jtc */
395 1.1 jtc pids = 1;
396 1.1 jtc break;
397 1.1 jtc case 'p':
398 1.1 jtc /*
399 1.35 lukem * preserve file mode bits
400 1.1 jtc */
401 1.1 jtc pmode = 1;
402 1.1 jtc break;
403 1.1 jtc default:
404 1.40 christos tty_warn(1, "Invalid -p string: %c",
405 1.40 christos *pt);
406 1.1 jtc pax_usage();
407 1.1 jtc break;
408 1.1 jtc }
409 1.1 jtc }
410 1.1 jtc flg |= PF;
411 1.1 jtc break;
412 1.1 jtc case 'r':
413 1.1 jtc /*
414 1.1 jtc * read the archive
415 1.1 jtc */
416 1.1 jtc flg |= RF;
417 1.1 jtc break;
418 1.1 jtc case 's':
419 1.1 jtc /*
420 1.1 jtc * file name substitution name pattern
421 1.1 jtc */
422 1.1 jtc if (rep_add(optarg) < 0) {
423 1.1 jtc pax_usage();
424 1.1 jtc break;
425 1.1 jtc }
426 1.1 jtc flg |= SF;
427 1.1 jtc break;
428 1.1 jtc case 't':
429 1.1 jtc /*
430 1.1 jtc * preserve access time on filesystem nodes we read
431 1.1 jtc */
432 1.1 jtc tflag = 1;
433 1.1 jtc flg |= TF;
434 1.1 jtc break;
435 1.1 jtc case 'u':
436 1.1 jtc /*
437 1.1 jtc * ignore those older files
438 1.1 jtc */
439 1.1 jtc uflag = 1;
440 1.1 jtc flg |= UF;
441 1.1 jtc break;
442 1.1 jtc case 'v':
443 1.1 jtc /*
444 1.1 jtc * verbose operation mode
445 1.1 jtc */
446 1.1 jtc vflag = 1;
447 1.1 jtc flg |= VF;
448 1.1 jtc break;
449 1.1 jtc case 'w':
450 1.1 jtc /*
451 1.1 jtc * write an archive
452 1.1 jtc */
453 1.1 jtc flg |= WF;
454 1.1 jtc break;
455 1.1 jtc case 'x':
456 1.1 jtc /*
457 1.1 jtc * specify an archive format on write
458 1.1 jtc */
459 1.1 jtc tmp.name = optarg;
460 1.8 christos frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
461 1.8 christos sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
462 1.8 christos if (frmt != NULL) {
463 1.1 jtc flg |= XF;
464 1.1 jtc break;
465 1.1 jtc }
466 1.8 christos tty_warn(1, "Unknown -x format: %s", optarg);
467 1.1 jtc (void)fputs("pax: Known -x formats are:", stderr);
468 1.1 jtc for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
469 1.1 jtc (void)fprintf(stderr, " %s", fsub[i].name);
470 1.1 jtc (void)fputs("\n\n", stderr);
471 1.1 jtc pax_usage();
472 1.1 jtc break;
473 1.6 mrg case 'z':
474 1.6 mrg /*
475 1.6 mrg * use gzip. Non standard option.
476 1.6 mrg */
477 1.6 mrg gzip_program = GZIP_CMD;
478 1.20 tron break;
479 1.20 tron case 'A':
480 1.20 tron Aflag = 1;
481 1.20 tron flg |= CAF;
482 1.6 mrg break;
483 1.1 jtc case 'B':
484 1.1 jtc /*
485 1.1 jtc * non-standard option on number of bytes written on a
486 1.1 jtc * single archive volume.
487 1.1 jtc */
488 1.1 jtc if ((wrlimit = str_offt(optarg)) <= 0) {
489 1.8 christos tty_warn(1, "Invalid write limit %s", optarg);
490 1.1 jtc pax_usage();
491 1.1 jtc }
492 1.1 jtc if (wrlimit % BLKMULT) {
493 1.8 christos tty_warn(1,
494 1.8 christos "Write limit is not a %d byte multiple",
495 1.1 jtc BLKMULT);
496 1.1 jtc pax_usage();
497 1.1 jtc }
498 1.1 jtc flg |= CBF;
499 1.1 jtc break;
500 1.1 jtc case 'D':
501 1.1 jtc /*
502 1.1 jtc * On extraction check file inode change time before the
503 1.1 jtc * modification of the file name. Non standard option.
504 1.1 jtc */
505 1.1 jtc Dflag = 1;
506 1.1 jtc flg |= CDF;
507 1.1 jtc break;
508 1.1 jtc case 'E':
509 1.1 jtc /*
510 1.1 jtc * non-standard limit on read faults
511 1.1 jtc * 0 indicates stop after first error, values
512 1.74 christos * indicate a limit, "none" try forever
513 1.1 jtc */
514 1.1 jtc flg |= CEF;
515 1.74 christos if (strcmp(none, optarg) == 0)
516 1.1 jtc maxflt = -1;
517 1.1 jtc else if ((maxflt = atoi(optarg)) < 0) {
518 1.8 christos tty_warn(1,
519 1.8 christos "Error count value must be positive");
520 1.1 jtc pax_usage();
521 1.1 jtc }
522 1.1 jtc break;
523 1.1 jtc case 'G':
524 1.1 jtc /*
525 1.1 jtc * non-standard option for selecting files within an
526 1.1 jtc * archive by group (gid or name)
527 1.1 jtc */
528 1.1 jtc if (grp_add(optarg) < 0) {
529 1.1 jtc pax_usage();
530 1.1 jtc break;
531 1.1 jtc }
532 1.1 jtc flg |= CGF;
533 1.1 jtc break;
534 1.1 jtc case 'H':
535 1.1 jtc /*
536 1.1 jtc * follow command line symlinks only
537 1.1 jtc */
538 1.1 jtc Hflag = 1;
539 1.1 jtc flg |= CHF;
540 1.1 jtc break;
541 1.1 jtc case 'L':
542 1.1 jtc /*
543 1.1 jtc * follow symlinks
544 1.1 jtc */
545 1.1 jtc Lflag = 1;
546 1.1 jtc flg |= CLF;
547 1.27 thorpej break;
548 1.39 lukem #ifdef SMALL
549 1.39 lukem case 'M':
550 1.39 lukem case 'N':
551 1.39 lukem tty_warn(1, "Support for -%c is not compiled in", c);
552 1.39 lukem exit(1);
553 1.39 lukem #else /* !SMALL */
554 1.34 lukem case 'M':
555 1.34 lukem /*
556 1.34 lukem * Treat list of filenames on stdin as an
557 1.34 lukem * mtree(8) specfile. Non standard option.
558 1.34 lukem */
559 1.34 lukem Mflag = 1;
560 1.34 lukem flg |= CMF;
561 1.34 lukem break;
562 1.35 lukem case 'N':
563 1.35 lukem /*
564 1.35 lukem * Use alternative directory for user db lookups.
565 1.35 lukem */
566 1.36 lukem if (!setup_getid(optarg)) {
567 1.36 lukem tty_warn(1,
568 1.36 lukem "Unable to use user and group databases in `%s'",
569 1.36 lukem optarg);
570 1.35 lukem pax_usage();
571 1.36 lukem }
572 1.35 lukem break;
573 1.39 lukem #endif /* !SMALL */
574 1.27 thorpej case 'O':
575 1.27 thorpej /*
576 1.27 thorpej * Force one volume. Non standard option.
577 1.27 thorpej */
578 1.27 thorpej force_one_volume = 1;
579 1.1 jtc break;
580 1.1 jtc case 'P':
581 1.1 jtc /*
582 1.1 jtc * do NOT follow symlinks (default)
583 1.1 jtc */
584 1.1 jtc Lflag = 0;
585 1.1 jtc flg |= CPF;
586 1.1 jtc break;
587 1.1 jtc case 'T':
588 1.1 jtc /*
589 1.1 jtc * non-standard option for selecting files within an
590 1.1 jtc * archive by modification time range (lower,upper)
591 1.1 jtc */
592 1.1 jtc if (trng_add(optarg) < 0) {
593 1.1 jtc pax_usage();
594 1.1 jtc break;
595 1.1 jtc }
596 1.1 jtc flg |= CTF;
597 1.1 jtc break;
598 1.1 jtc case 'U':
599 1.1 jtc /*
600 1.1 jtc * non-standard option for selecting files within an
601 1.1 jtc * archive by user (uid or name)
602 1.1 jtc */
603 1.1 jtc if (usr_add(optarg) < 0) {
604 1.1 jtc pax_usage();
605 1.1 jtc break;
606 1.1 jtc }
607 1.1 jtc flg |= CUF;
608 1.1 jtc break;
609 1.1 jtc case 'X':
610 1.1 jtc /*
611 1.1 jtc * do not pass over mount points in the file system
612 1.1 jtc */
613 1.1 jtc Xflag = 1;
614 1.1 jtc flg |= CXF;
615 1.1 jtc break;
616 1.1 jtc case 'Y':
617 1.1 jtc /*
618 1.1 jtc * On extraction check file inode change time after the
619 1.1 jtc * modification of the file name. Non standard option.
620 1.1 jtc */
621 1.1 jtc Yflag = 1;
622 1.1 jtc flg |= CYF;
623 1.1 jtc break;
624 1.1 jtc case 'Z':
625 1.1 jtc /*
626 1.1 jtc * On extraction check modification time after the
627 1.1 jtc * modification of the file name. Non standard option.
628 1.1 jtc */
629 1.1 jtc Zflag = 1;
630 1.1 jtc flg |= CZF;
631 1.1 jtc break;
632 1.47 christos case OPT_INSECURE:
633 1.47 christos secure = 0;
634 1.47 christos break;
635 1.50 christos case OPT_FORCE_LOCAL:
636 1.50 christos forcelocal = 0;
637 1.50 christos break;
638 1.1 jtc case '?':
639 1.1 jtc default:
640 1.1 jtc pax_usage();
641 1.1 jtc break;
642 1.1 jtc }
643 1.1 jtc }
644 1.1 jtc
645 1.1 jtc /*
646 1.1 jtc * figure out the operation mode of pax read,write,extract,copy,append
647 1.1 jtc * or list. check that we have not been given a bogus set of flags
648 1.1 jtc * for the operation mode.
649 1.1 jtc */
650 1.1 jtc if (ISLIST(flg)) {
651 1.1 jtc act = LIST;
652 1.40 christos listf = stdout;
653 1.1 jtc bflg = flg & BDLIST;
654 1.1 jtc } else if (ISEXTRACT(flg)) {
655 1.1 jtc act = EXTRACT;
656 1.1 jtc bflg = flg & BDEXTR;
657 1.1 jtc } else if (ISARCHIVE(flg)) {
658 1.1 jtc act = ARCHIVE;
659 1.1 jtc bflg = flg & BDARCH;
660 1.1 jtc } else if (ISAPPND(flg)) {
661 1.1 jtc act = APPND;
662 1.1 jtc bflg = flg & BDARCH;
663 1.1 jtc } else if (ISCOPY(flg)) {
664 1.1 jtc act = COPY;
665 1.1 jtc bflg = flg & BDCOPY;
666 1.1 jtc } else
667 1.1 jtc pax_usage();
668 1.1 jtc if (bflg) {
669 1.1 jtc printflg(flg);
670 1.1 jtc pax_usage();
671 1.1 jtc }
672 1.1 jtc
673 1.1 jtc /*
674 1.1 jtc * if we are writing (ARCHIVE) we use the default format if the user
675 1.1 jtc * did not specify a format. when we write during an APPEND, we will
676 1.1 jtc * adopt the format of the existing archive if none was supplied.
677 1.1 jtc */
678 1.1 jtc if (!(flg & XF) && (act == ARCHIVE))
679 1.1 jtc frmt = &(fsub[DEFLT]);
680 1.1 jtc
681 1.1 jtc /*
682 1.1 jtc * process the args as they are interpreted by the operation mode
683 1.1 jtc */
684 1.1 jtc switch (act) {
685 1.1 jtc case LIST:
686 1.1 jtc case EXTRACT:
687 1.1 jtc for (; optind < argc; optind++)
688 1.40 christos if (pat_add(argv[optind], NULL) < 0)
689 1.1 jtc pax_usage();
690 1.1 jtc break;
691 1.1 jtc case COPY:
692 1.1 jtc if (optind >= argc) {
693 1.8 christos tty_warn(0, "Destination directory was not supplied");
694 1.1 jtc pax_usage();
695 1.1 jtc }
696 1.42 mrg --argc;
697 1.42 mrg dirptr = argv[argc];
698 1.40 christos if (mkpath(dirptr) < 0)
699 1.90 christos exit(1);
700 1.14 mycroft /* FALLTHROUGH */
701 1.1 jtc case ARCHIVE:
702 1.1 jtc case APPND:
703 1.1 jtc for (; optind < argc; optind++)
704 1.23 is if (ftree_add(argv[optind], 0) < 0)
705 1.1 jtc pax_usage();
706 1.1 jtc /*
707 1.1 jtc * no read errors allowed on updates/append operation!
708 1.1 jtc */
709 1.1 jtc maxflt = 0;
710 1.1 jtc break;
711 1.1 jtc }
712 1.1 jtc }
713 1.1 jtc
714 1.1 jtc
715 1.1 jtc /*
716 1.1 jtc * tar_options()
717 1.1 jtc * look at the user specified flags. set globals as required and check if
718 1.1 jtc * the user specified a legal set of flags. If not, complain and exit
719 1.1 jtc */
720 1.1 jtc
721 1.29 thorpej struct option tar_longopts[] = {
722 1.29 thorpej { "block-size", required_argument, 0, 'b' },
723 1.62 wiz { "bunzip2", no_argument, 0, 'j' },
724 1.62 wiz { "bzip2", no_argument, 0, 'j' },
725 1.29 thorpej { "create", no_argument, 0, 'c' }, /* F */
726 1.29 thorpej /* -e -- no corresponding long option */
727 1.29 thorpej { "file", required_argument, 0, 'f' },
728 1.29 thorpej { "dereference", no_argument, 0, 'h' },
729 1.64 christos { "keep-old-files", no_argument, 0, 'k' },
730 1.29 thorpej { "one-file-system", no_argument, 0, 'l' },
731 1.29 thorpej { "modification-time", no_argument, 0, 'm' },
732 1.29 thorpej { "old-archive", no_argument, 0, 'o' },
733 1.29 thorpej { "portability", no_argument, 0, 'o' },
734 1.29 thorpej { "same-permissions", no_argument, 0, 'p' },
735 1.29 thorpej { "preserve-permissions", no_argument, 0, 'p' },
736 1.29 thorpej { "preserve", no_argument, 0, 'p' },
737 1.46 christos { "fast-read", no_argument, 0, 'q' },
738 1.29 thorpej { "append", no_argument, 0, 'r' }, /* F */
739 1.29 thorpej { "update", no_argument, 0, 'u' }, /* F */
740 1.29 thorpej { "list", no_argument, 0, 't' }, /* F */
741 1.29 thorpej { "verbose", no_argument, 0, 'v' },
742 1.29 thorpej { "interactive", no_argument, 0, 'w' },
743 1.29 thorpej { "confirmation", no_argument, 0, 'w' },
744 1.29 thorpej { "extract", no_argument, 0, 'x' }, /* F */
745 1.29 thorpej { "get", no_argument, 0, 'x' }, /* F */
746 1.29 thorpej { "gzip", no_argument, 0, 'z' },
747 1.29 thorpej { "gunzip", no_argument, 0, 'z' },
748 1.29 thorpej { "read-full-blocks", no_argument, 0, 'B' },
749 1.29 thorpej { "directory", required_argument, 0, 'C' },
750 1.65 simonb { "to-stdout", no_argument, 0, 'O' },
751 1.29 thorpej { "absolute-paths", no_argument, 0, 'P' },
752 1.80 dsl { "sparse", no_argument, 0, 'S' },
753 1.40 christos { "files-from", required_argument, 0, 'T' },
754 1.29 thorpej { "exclude-from", required_argument, 0, 'X' },
755 1.29 thorpej { "compress", no_argument, 0, 'Z' },
756 1.29 thorpej { "uncompress", no_argument, 0, 'Z' },
757 1.48 christos { "strict", no_argument, 0,
758 1.48 christos OPT_STRICT },
759 1.31 thorpej { "atime-preserve", no_argument, 0,
760 1.31 thorpej OPT_ATIME_PRESERVE },
761 1.29 thorpej { "unlink", no_argument, 0,
762 1.29 thorpej OPT_UNLINK },
763 1.30 thorpej { "use-compress-program", required_argument, 0,
764 1.30 thorpej OPT_USE_COMPRESS_PROGRAM },
765 1.40 christos { "force-local", no_argument, 0,
766 1.40 christos OPT_FORCE_LOCAL },
767 1.47 christos { "insecure", no_argument, 0,
768 1.47 christos OPT_INSECURE },
769 1.57 mrg { "exclude", required_argument, 0,
770 1.57 mrg OPT_EXCLUDE },
771 1.86 christos { "no-recursion", no_argument, 0,
772 1.86 christos OPT_NORECURSE },
773 1.81 jmc #if !HAVE_NBTOOL_CONFIG_H
774 1.80 dsl { "chroot", no_argument, 0,
775 1.80 dsl OPT_CHROOT },
776 1.81 jmc #endif
777 1.29 thorpej #if 0 /* Not implemented */
778 1.29 thorpej { "catenate", no_argument, 0, 'A' }, /* F */
779 1.29 thorpej { "concatenate", no_argument, 0, 'A' }, /* F */
780 1.29 thorpej { "diff", no_argument, 0, 'd' }, /* F */
781 1.29 thorpej { "compare", no_argument, 0, 'd' }, /* F */
782 1.29 thorpej { "checkpoint", no_argument, 0,
783 1.29 thorpej OPT_CHECKPOINT },
784 1.31 thorpej { "help", no_argument, 0,
785 1.29 thorpej OPT_HELP },
786 1.29 thorpej { "info-script", required_argument, 0, 'F' },
787 1.29 thorpej { "new-volume-script", required_argument, 0, 'F' },
788 1.29 thorpej { "incremental", no_argument, 0, 'G' },
789 1.29 thorpej { "listed-incremental", required_argument, 0, 'g' },
790 1.29 thorpej { "ignore-zeros", no_argument, 0, 'i' },
791 1.29 thorpej { "ignore-failed-read", no_argument, 0,
792 1.29 thorpej OPT_IGNORE_FAILED_READ },
793 1.29 thorpej { "starting-file", no_argument, 0, 'K' },
794 1.50 christos { "tape-length", required_argument, 0, 'L' },
795 1.29 thorpej { "multi-volume", no_argument, 0, 'M' },
796 1.29 thorpej { "after-date", required_argument, 0, 'N' },
797 1.29 thorpej { "newer", required_argument, 0, 'N' },
798 1.29 thorpej { "record-number", no_argument, 0, 'R' },
799 1.29 thorpej { "remove-files", no_argument, 0,
800 1.29 thorpej OPT_REMOVE_FILES },
801 1.29 thorpej { "same-order", no_argument, 0, 's' },
802 1.29 thorpej { "preserve-order", no_argument, 0, 's' },
803 1.29 thorpej { "null", no_argument, 0,
804 1.29 thorpej OPT_NULL },
805 1.29 thorpej { "totals", no_argument, 0,
806 1.29 thorpej OPT_TOTALS },
807 1.29 thorpej { "volume-name", required_argument, 0, 'V' },
808 1.29 thorpej { "label", required_argument, 0, 'V' },
809 1.29 thorpej { "version", no_argument, 0,
810 1.29 thorpej OPT_VERSION },
811 1.29 thorpej { "verify", no_argument, 0, 'W' },
812 1.29 thorpej { "block-compress", no_argument, 0,
813 1.29 thorpej OPT_BLOCK_COMPRESS },
814 1.29 thorpej #endif
815 1.29 thorpej { 0, 0, 0, 0 },
816 1.29 thorpej };
817 1.29 thorpej
818 1.1 jtc static void
819 1.7 tls tar_options(int argc, char **argv)
820 1.1 jtc {
821 1.7 tls int c;
822 1.1 jtc int fstdin = 0;
823 1.40 christos int Oflag = 0;
824 1.40 christos int nincfiles = 0;
825 1.40 christos int incfiles_max = 0;
826 1.40 christos struct incfile {
827 1.40 christos char *file;
828 1.40 christos char *dir;
829 1.40 christos };
830 1.40 christos struct incfile *incfiles = NULL;
831 1.40 christos
832 1.40 christos /*
833 1.40 christos * Set default values.
834 1.40 christos */
835 1.40 christos rmleadslash = 1;
836 1.49 christos is_gnutar = 1;
837 1.1 jtc
838 1.1 jtc /*
839 1.1 jtc * process option flags
840 1.1 jtc */
841 1.40 christos while ((c = getoldopt(argc, argv,
842 1.78 christos "+b:cef:hjklmopqrs:tuvwxzBC:HI:OPST:X:Z014578",
843 1.29 thorpej tar_longopts, NULL))
844 1.9 lukem != -1) {
845 1.4 jtc switch(c) {
846 1.1 jtc case 'b':
847 1.1 jtc /*
848 1.40 christos * specify blocksize in 512-byte blocks
849 1.1 jtc */
850 1.4 jtc if ((wrblksz = (int)str_offt(optarg)) <= 0) {
851 1.8 christos tty_warn(1, "Invalid block size %s", optarg);
852 1.1 jtc tar_usage();
853 1.1 jtc }
854 1.40 christos wrblksz *= 512; /* XXX - check for int oflow */
855 1.1 jtc break;
856 1.1 jtc case 'c':
857 1.1 jtc /*
858 1.1 jtc * create an archive
859 1.1 jtc */
860 1.1 jtc act = ARCHIVE;
861 1.1 jtc break;
862 1.1 jtc case 'e':
863 1.1 jtc /*
864 1.1 jtc * stop after first error
865 1.1 jtc */
866 1.1 jtc maxflt = 0;
867 1.1 jtc break;
868 1.1 jtc case 'f':
869 1.1 jtc /*
870 1.1 jtc * filename where the archive is stored
871 1.1 jtc */
872 1.4 jtc if ((optarg[0] == '-') && (optarg[1]== '\0')) {
873 1.1 jtc /*
874 1.1 jtc * treat a - as stdin
875 1.1 jtc */
876 1.4 jtc fstdin = 1;
877 1.40 christos arcname = NULL;
878 1.1 jtc break;
879 1.1 jtc }
880 1.1 jtc fstdin = 0;
881 1.4 jtc arcname = optarg;
882 1.1 jtc break;
883 1.21 tron case 'h':
884 1.21 tron /*
885 1.58 christos * follow symlinks
886 1.21 tron */
887 1.58 christos Lflag = 1;
888 1.21 tron break;
889 1.62 wiz case 'j':
890 1.62 wiz /*
891 1.62 wiz * pass through bzip2. not a standard option
892 1.62 wiz */
893 1.62 wiz jflag = 1;
894 1.62 wiz gzip_program = BZIP2_CMD;
895 1.64 christos break;
896 1.64 christos case 'k':
897 1.64 christos /*
898 1.64 christos * do not clobber files that exist
899 1.64 christos */
900 1.64 christos kflag = 1;
901 1.62 wiz break;
902 1.10 mrg case 'l':
903 1.10 mrg /*
904 1.10 mrg * do not pass over mount points in the file system
905 1.10 mrg */
906 1.10 mrg Xflag = 1;
907 1.10 mrg break;
908 1.1 jtc case 'm':
909 1.1 jtc /*
910 1.1 jtc * do not preserve modification time
911 1.1 jtc */
912 1.1 jtc pmtime = 0;
913 1.1 jtc break;
914 1.1 jtc case 'o':
915 1.18 tv /*
916 1.18 tv * This option does several things based on whether
917 1.18 tv * this is a create or extract operation.
918 1.18 tv */
919 1.18 tv if (act == ARCHIVE) {
920 1.48 christos /* GNU tar: write V7 format archives. */
921 1.48 christos Oflag = 1;
922 1.18 tv /* 4.2BSD: don't add directory entries. */
923 1.18 tv if (opt_add("write_opt=nodir") < 0)
924 1.18 tv tar_usage();
925 1.18 tv
926 1.18 tv } else {
927 1.18 tv /* SUS: don't preserve owner/group. */
928 1.18 tv pids = 0;
929 1.18 tv nopids = 1;
930 1.18 tv }
931 1.1 jtc break;
932 1.40 christos case 'O':
933 1.40 christos Oflag = 1;
934 1.40 christos break;
935 1.1 jtc case 'p':
936 1.1 jtc /*
937 1.1 jtc * preserve user id, group id, file
938 1.1 jtc * mode, access/modification times
939 1.1 jtc */
940 1.18 tv if (!nopids)
941 1.18 tv pids = 1;
942 1.1 jtc pmode = 1;
943 1.1 jtc patime = 1;
944 1.1 jtc pmtime = 1;
945 1.1 jtc break;
946 1.40 christos case 'q':
947 1.40 christos /*
948 1.40 christos * select first match for a pattern only
949 1.40 christos */
950 1.40 christos nflag = 1;
951 1.40 christos break;
952 1.1 jtc case 'r':
953 1.1 jtc case 'u':
954 1.1 jtc /*
955 1.1 jtc * append to the archive
956 1.1 jtc */
957 1.1 jtc act = APPND;
958 1.1 jtc break;
959 1.40 christos case 's':
960 1.40 christos /*
961 1.40 christos * file name substitution name pattern
962 1.40 christos */
963 1.40 christos if (rep_add(optarg) < 0) {
964 1.40 christos tar_usage();
965 1.40 christos break;
966 1.40 christos }
967 1.40 christos break;
968 1.1 jtc case 't':
969 1.1 jtc /*
970 1.1 jtc * list contents of the tape
971 1.1 jtc */
972 1.1 jtc act = LIST;
973 1.1 jtc break;
974 1.1 jtc case 'v':
975 1.1 jtc /*
976 1.1 jtc * verbose operation mode
977 1.1 jtc */
978 1.1 jtc vflag = 1;
979 1.1 jtc break;
980 1.1 jtc case 'w':
981 1.1 jtc /*
982 1.1 jtc * interactive file rename
983 1.1 jtc */
984 1.1 jtc iflag = 1;
985 1.1 jtc break;
986 1.1 jtc case 'x':
987 1.1 jtc /*
988 1.40 christos * extract an archive, preserving mode,
989 1.40 christos * and mtime if possible.
990 1.1 jtc */
991 1.1 jtc act = EXTRACT;
992 1.40 christos pmtime = 1;
993 1.1 jtc break;
994 1.6 mrg case 'z':
995 1.6 mrg /*
996 1.6 mrg * use gzip. Non standard option.
997 1.6 mrg */
998 1.6 mrg zflag = 1;
999 1.6 mrg gzip_program = GZIP_CMD;
1000 1.6 mrg break;
1001 1.1 jtc case 'B':
1002 1.1 jtc /*
1003 1.1 jtc * Nothing to do here, this is pax default
1004 1.1 jtc */
1005 1.1 jtc break;
1006 1.40 christos case 'C':
1007 1.97 christos havechd++;
1008 1.40 christos chdname = optarg;
1009 1.40 christos break;
1010 1.40 christos case 'H':
1011 1.40 christos /*
1012 1.40 christos * follow command line symlinks only
1013 1.40 christos */
1014 1.40 christos Hflag = 1;
1015 1.40 christos break;
1016 1.40 christos case 'I':
1017 1.40 christos case 'T':
1018 1.40 christos if (++nincfiles > incfiles_max) {
1019 1.40 christos incfiles_max = nincfiles + 3;
1020 1.40 christos incfiles = realloc(incfiles,
1021 1.40 christos sizeof(*incfiles) * incfiles_max);
1022 1.40 christos if (incfiles == NULL) {
1023 1.40 christos tty_warn(0, "Unable to allocate space "
1024 1.40 christos "for option list");
1025 1.40 christos exit(1);
1026 1.40 christos }
1027 1.40 christos }
1028 1.40 christos incfiles[nincfiles - 1].file = optarg;
1029 1.40 christos incfiles[nincfiles - 1].dir = chdname;
1030 1.1 jtc break;
1031 1.1 jtc case 'P':
1032 1.40 christos /*
1033 1.40 christos * do not remove leading '/' from pathnames
1034 1.40 christos */
1035 1.40 christos rmleadslash = 0;
1036 1.21 tron Aflag = 1;
1037 1.1 jtc break;
1038 1.78 christos case 'S':
1039 1.78 christos /* do nothing; we already generate sparse files */
1040 1.78 christos break;
1041 1.1 jtc case 'X':
1042 1.1 jtc /*
1043 1.15 mrg * GNU tar compat: exclude the files listed in optarg
1044 1.1 jtc */
1045 1.15 mrg if (tar_gnutar_X_compat(optarg) != 0)
1046 1.15 mrg tar_usage();
1047 1.6 mrg break;
1048 1.6 mrg case 'Z':
1049 1.6 mrg /*
1050 1.6 mrg * use compress.
1051 1.6 mrg */
1052 1.6 mrg zflag = 1;
1053 1.6 mrg gzip_program = COMPRESS_CMD;
1054 1.1 jtc break;
1055 1.1 jtc case '0':
1056 1.1 jtc arcname = DEV_0;
1057 1.1 jtc break;
1058 1.1 jtc case '1':
1059 1.1 jtc arcname = DEV_1;
1060 1.1 jtc break;
1061 1.1 jtc case '4':
1062 1.1 jtc arcname = DEV_4;
1063 1.1 jtc break;
1064 1.1 jtc case '5':
1065 1.1 jtc arcname = DEV_5;
1066 1.1 jtc break;
1067 1.1 jtc case '7':
1068 1.1 jtc arcname = DEV_7;
1069 1.1 jtc break;
1070 1.1 jtc case '8':
1071 1.1 jtc arcname = DEV_8;
1072 1.31 thorpej break;
1073 1.31 thorpej case OPT_ATIME_PRESERVE:
1074 1.31 thorpej patime = 1;
1075 1.1 jtc break;
1076 1.29 thorpej case OPT_UNLINK:
1077 1.29 thorpej /* Just ignore -- we always unlink first. */
1078 1.30 thorpej break;
1079 1.30 thorpej case OPT_USE_COMPRESS_PROGRAM:
1080 1.30 thorpej zflag = 1;
1081 1.30 thorpej gzip_program = optarg;
1082 1.29 thorpej break;
1083 1.40 christos case OPT_FORCE_LOCAL:
1084 1.40 christos forcelocal = 1;
1085 1.40 christos break;
1086 1.47 christos case OPT_INSECURE:
1087 1.47 christos secure = 0;
1088 1.47 christos break;
1089 1.48 christos case OPT_STRICT:
1090 1.48 christos /* disable gnu extensions */
1091 1.48 christos is_gnutar = 0;
1092 1.57 mrg break;
1093 1.57 mrg case OPT_EXCLUDE:
1094 1.57 mrg if (tar_gnutar_minus_minus_exclude(optarg) != 0)
1095 1.57 mrg tar_usage();
1096 1.48 christos break;
1097 1.86 christos case OPT_NORECURSE:
1098 1.86 christos dflag = 1;
1099 1.86 christos break;
1100 1.81 jmc #if !HAVE_NBTOOL_CONFIG_H
1101 1.80 dsl case OPT_CHROOT:
1102 1.80 dsl do_chroot = 1;
1103 1.80 dsl break;
1104 1.81 jmc #endif
1105 1.1 jtc default:
1106 1.1 jtc tar_usage();
1107 1.1 jtc break;
1108 1.1 jtc }
1109 1.1 jtc }
1110 1.4 jtc argc -= optind;
1111 1.4 jtc argv += optind;
1112 1.52 soren
1113 1.52 soren /* Tar requires an action. */
1114 1.54 christos if (act == ERROR)
1115 1.52 soren tar_usage();
1116 1.17 tv
1117 1.40 christos /* Traditional tar behaviour (pax uses stderr unless in list mode) */
1118 1.40 christos if (fstdin == 1 && act == ARCHIVE)
1119 1.40 christos listf = stderr;
1120 1.40 christos else
1121 1.40 christos listf = stdout;
1122 1.23 is
1123 1.40 christos /* Traditional tar behaviour (pax wants to read file list from stdin) */
1124 1.40 christos if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
1125 1.40 christos exit(0);
1126 1.17 tv /*
1127 1.17 tv * if we are writing (ARCHIVE) specify tar, otherwise run like pax
1128 1.40 christos * (unless -o specified)
1129 1.17 tv */
1130 1.40 christos if (act == ARCHIVE || act == APPND)
1131 1.40 christos frmt = &(fsub[Oflag ? F_TAR : F_USTAR]);
1132 1.40 christos else if (Oflag) {
1133 1.65 simonb if (act == EXTRACT)
1134 1.65 simonb to_stdout = 1;
1135 1.65 simonb else {
1136 1.65 simonb tty_warn(1, "The -O/-o options are only valid when "
1137 1.65 simonb "writing or extracting an archive");
1138 1.65 simonb tar_usage();
1139 1.65 simonb }
1140 1.40 christos }
1141 1.1 jtc
1142 1.1 jtc /*
1143 1.1 jtc * process the args as they are interpreted by the operation mode
1144 1.1 jtc */
1145 1.1 jtc switch (act) {
1146 1.1 jtc case LIST:
1147 1.40 christos case EXTRACT:
1148 1.1 jtc default:
1149 1.40 christos {
1150 1.40 christos int sawpat = 0;
1151 1.55 christos int dirisnext = 0;
1152 1.95 christos char *file, *dir = NULL;
1153 1.40 christos
1154 1.40 christos while (nincfiles || *argv != NULL) {
1155 1.40 christos /*
1156 1.40 christos * If we queued up any include files,
1157 1.40 christos * pull them in now. Otherwise, check
1158 1.40 christos * for -I and -C positional flags.
1159 1.40 christos * Anything else must be a file to
1160 1.40 christos * extract.
1161 1.40 christos */
1162 1.40 christos if (nincfiles) {
1163 1.40 christos file = incfiles->file;
1164 1.40 christos dir = incfiles->dir;
1165 1.40 christos incfiles++;
1166 1.40 christos nincfiles--;
1167 1.40 christos } else if (strcmp(*argv, "-I") == 0) {
1168 1.40 christos if (*++argv == NULL)
1169 1.40 christos break;
1170 1.40 christos file = *argv++;
1171 1.40 christos dir = chdname;
1172 1.88 lukem } else {
1173 1.40 christos file = NULL;
1174 1.88 lukem dir = NULL;
1175 1.88 lukem }
1176 1.40 christos if (file != NULL) {
1177 1.40 christos FILE *fp;
1178 1.40 christos char *str;
1179 1.40 christos
1180 1.40 christos if (strcmp(file, "-") == 0)
1181 1.40 christos fp = stdin;
1182 1.40 christos else if ((fp = fopen(file, "r")) == NULL) {
1183 1.40 christos tty_warn(1, "Unable to open file '%s' for read", file);
1184 1.40 christos tar_usage();
1185 1.40 christos }
1186 1.40 christos while ((str = getline(fp)) != NULL) {
1187 1.55 christos if (dirisnext) {
1188 1.95 christos if (dir)
1189 1.95 christos free(dir);
1190 1.55 christos dir = str;
1191 1.55 christos dirisnext = 0;
1192 1.55 christos continue;
1193 1.55 christos }
1194 1.55 christos if (strcmp(str, "-C") == 0) {
1195 1.97 christos havechd++;
1196 1.55 christos dirisnext = 1;
1197 1.94 christos free(str);
1198 1.55 christos continue;
1199 1.55 christos }
1200 1.79 christos if (strncmp(str, "-C ", 3) == 0) {
1201 1.97 christos havechd++;
1202 1.95 christos if (dir)
1203 1.95 christos free(dir);
1204 1.94 christos dir = strdup(str + 3);
1205 1.94 christos free(str);
1206 1.79 christos continue;
1207 1.79 christos }
1208 1.40 christos if (pat_add(str, dir) < 0)
1209 1.40 christos tar_usage();
1210 1.40 christos sawpat = 1;
1211 1.40 christos }
1212 1.55 christos /* Bomb if given -C w/out a dir. */
1213 1.55 christos if (dirisnext)
1214 1.55 christos tar_usage();
1215 1.95 christos if (dir)
1216 1.95 christos free(dir);
1217 1.40 christos if (strcmp(file, "-") != 0)
1218 1.40 christos fclose(fp);
1219 1.40 christos if (getline_error) {
1220 1.40 christos tty_warn(1, "Problem with file '%s'", file);
1221 1.40 christos tar_usage();
1222 1.40 christos }
1223 1.40 christos } else if (strcmp(*argv, "-C") == 0) {
1224 1.40 christos if (*++argv == NULL)
1225 1.40 christos break;
1226 1.40 christos chdname = *argv++;
1227 1.97 christos havechd++;
1228 1.40 christos } else if (pat_add(*argv++, chdname) < 0)
1229 1.40 christos tar_usage();
1230 1.40 christos else
1231 1.40 christos sawpat = 1;
1232 1.40 christos }
1233 1.40 christos /*
1234 1.40 christos * if patterns were added, we are doing chdir()
1235 1.40 christos * on a file-by-file basis, else, just one
1236 1.40 christos * global chdir (if any) after opening input.
1237 1.40 christos */
1238 1.40 christos if (sawpat > 0)
1239 1.40 christos chdname = NULL;
1240 1.40 christos }
1241 1.23 is break;
1242 1.1 jtc case ARCHIVE:
1243 1.1 jtc case APPND:
1244 1.40 christos if (chdname != NULL) { /* initial chdir() */
1245 1.40 christos if (ftree_add(chdname, 1) < 0)
1246 1.40 christos tar_usage();
1247 1.40 christos }
1248 1.40 christos
1249 1.40 christos while (nincfiles || *argv != NULL) {
1250 1.40 christos char *file, *dir;
1251 1.40 christos
1252 1.40 christos /*
1253 1.40 christos * If we queued up any include files, pull them in
1254 1.40 christos * now. Otherwise, check for -I and -C positional
1255 1.40 christos * flags. Anything else must be a file to include
1256 1.40 christos * in the archive.
1257 1.40 christos */
1258 1.40 christos if (nincfiles) {
1259 1.40 christos file = incfiles->file;
1260 1.40 christos dir = incfiles->dir;
1261 1.40 christos incfiles++;
1262 1.40 christos nincfiles--;
1263 1.40 christos } else if (strcmp(*argv, "-I") == 0) {
1264 1.40 christos if (*++argv == NULL)
1265 1.40 christos break;
1266 1.40 christos file = *argv++;
1267 1.40 christos dir = NULL;
1268 1.88 lukem } else {
1269 1.40 christos file = NULL;
1270 1.88 lukem dir = NULL;
1271 1.88 lukem }
1272 1.40 christos if (file != NULL) {
1273 1.40 christos FILE *fp;
1274 1.40 christos char *str;
1275 1.55 christos int dirisnext = 0;
1276 1.40 christos
1277 1.40 christos /* Set directory if needed */
1278 1.40 christos if (dir) {
1279 1.40 christos if (ftree_add(dir, 1) < 0)
1280 1.40 christos tar_usage();
1281 1.40 christos }
1282 1.40 christos
1283 1.40 christos if (strcmp(file, "-") == 0)
1284 1.40 christos fp = stdin;
1285 1.40 christos else if ((fp = fopen(file, "r")) == NULL) {
1286 1.40 christos tty_warn(1, "Unable to open file '%s' for read", file);
1287 1.40 christos tar_usage();
1288 1.40 christos }
1289 1.40 christos while ((str = getline(fp)) != NULL) {
1290 1.55 christos if (dirisnext) {
1291 1.55 christos if (ftree_add(str, 1) < 0)
1292 1.55 christos tar_usage();
1293 1.55 christos dirisnext = 0;
1294 1.55 christos continue;
1295 1.55 christos }
1296 1.55 christos if (strcmp(str, "-C") == 0) {
1297 1.55 christos dirisnext = 1;
1298 1.55 christos continue;
1299 1.55 christos }
1300 1.79 christos if (strncmp(str, "-C ", 3) == 0) {
1301 1.79 christos if (ftree_add(str + 3, 1) < 0)
1302 1.79 christos tar_usage();
1303 1.79 christos continue;
1304 1.79 christos }
1305 1.40 christos if (ftree_add(str, 0) < 0)
1306 1.40 christos tar_usage();
1307 1.40 christos }
1308 1.55 christos /* Bomb if given -C w/out a dir. */
1309 1.55 christos if (dirisnext)
1310 1.55 christos tar_usage();
1311 1.40 christos if (strcmp(file, "-") != 0)
1312 1.40 christos fclose(fp);
1313 1.40 christos if (getline_error) {
1314 1.40 christos tty_warn(1, "Problem with file '%s'",
1315 1.40 christos file);
1316 1.40 christos tar_usage();
1317 1.40 christos }
1318 1.40 christos } else if (strcmp(*argv, "-C") == 0) {
1319 1.40 christos if (*++argv == NULL)
1320 1.40 christos break;
1321 1.40 christos if (ftree_add(*argv++, 1) < 0)
1322 1.40 christos tar_usage();
1323 1.40 christos } else if (ftree_add(*argv++, 0) < 0)
1324 1.40 christos tar_usage();
1325 1.40 christos }
1326 1.1 jtc /*
1327 1.1 jtc * no read errors allowed on updates/append operation!
1328 1.1 jtc */
1329 1.1 jtc maxflt = 0;
1330 1.1 jtc break;
1331 1.1 jtc }
1332 1.1 jtc if (!fstdin && ((arcname == (char *)NULL) || (*arcname == '\0'))) {
1333 1.1 jtc arcname = getenv("TAPE");
1334 1.40 christos if ((arcname == NULL) || (*arcname == '\0'))
1335 1.40 christos arcname = _PATH_DEFTAPE;
1336 1.40 christos }
1337 1.40 christos }
1338 1.40 christos
1339 1.40 christos int
1340 1.40 christos mkpath(path)
1341 1.40 christos char *path;
1342 1.40 christos {
1343 1.40 christos char *slash;
1344 1.92 christos int done = 0;
1345 1.40 christos
1346 1.40 christos slash = path;
1347 1.40 christos
1348 1.40 christos while (!done) {
1349 1.40 christos slash += strspn(slash, "/");
1350 1.40 christos slash += strcspn(slash, "/");
1351 1.40 christos
1352 1.40 christos done = (*slash == '\0');
1353 1.40 christos *slash = '\0';
1354 1.40 christos
1355 1.92 christos if (domkdir(path, 0777) == -1)
1356 1.91 christos goto out;
1357 1.40 christos
1358 1.40 christos if (!done)
1359 1.40 christos *slash = '/';
1360 1.1 jtc }
1361 1.40 christos
1362 1.90 christos return 0;
1363 1.91 christos out:
1364 1.91 christos /* Can't create or or not a directory */
1365 1.92 christos syswarn(1, errno, "Cannot create directory `%s'", path);
1366 1.91 christos return -1;
1367 1.1 jtc }
1368 1.1 jtc
1369 1.45 christos
1370 1.89 christos #ifndef NO_CPIO
1371 1.45 christos struct option cpio_longopts[] = {
1372 1.45 christos { "reset-access-time", no_argument, 0, 'a' },
1373 1.45 christos { "make-directories", no_argument, 0, 'd' },
1374 1.45 christos { "nonmatching", no_argument, 0, 'f' },
1375 1.45 christos { "extract", no_argument, 0, 'i' },
1376 1.45 christos { "link", no_argument, 0, 'l' },
1377 1.45 christos { "preserve-modification-time", no_argument, 0, 'm' },
1378 1.45 christos { "create", no_argument, 0, 'o' },
1379 1.45 christos { "pass-through", no_argument, 0, 'p' },
1380 1.45 christos { "rename", no_argument, 0, 'r' },
1381 1.45 christos { "list", no_argument, 0, 't' },
1382 1.45 christos { "unconditional", no_argument, 0, 'u' },
1383 1.45 christos { "verbose", no_argument, 0, 'v' },
1384 1.45 christos { "append", no_argument, 0, 'A' },
1385 1.45 christos { "pattern-file", required_argument, 0, 'E' },
1386 1.45 christos { "file", required_argument, 0, 'F' },
1387 1.45 christos { "force-local", no_argument, 0,
1388 1.47 christos OPT_FORCE_LOCAL },
1389 1.45 christos { "format", required_argument, 0, 'H' },
1390 1.45 christos { "dereference", no_argument, 0, 'L' },
1391 1.45 christos { "swap-halfwords", no_argument, 0, 'S' },
1392 1.47 christos { "insecure", no_argument, 0,
1393 1.47 christos OPT_INSECURE },
1394 1.78 christos { "sparse", no_argument, 0,
1395 1.78 christos OPT_SPARSE },
1396 1.45 christos
1397 1.45 christos #ifdef notyet
1398 1.45 christos /* Not implemented */
1399 1.45 christos { "null", no_argument, 0, '0' },
1400 1.45 christos { "swap", no_argument, 0, 'b' },
1401 1.45 christos { "numeric-uid-gid", no_argument, 0, 'n' },
1402 1.45 christos { "swap-bytes", no_argument, 0, 's' },
1403 1.45 christos { "message", required_argument, 0, 'M' },
1404 1.45 christos { "owner", required_argument, 0 'R' },
1405 1.45 christos { "dot", no_argument, 0, 'V' },
1406 1.45 christos { "block-size", required_argument, 0,
1407 1.47 christos OPT_BLOCK_SIZE },
1408 1.45 christos { "no-absolute-pathnames", no_argument, 0,
1409 1.47 christos OPT_NO_ABSOLUTE_PATHNAMES },
1410 1.45 christos { "no-preserve-owner", no_argument, 0,
1411 1.47 christos OPT_NO_PRESERVE_OWNER },
1412 1.45 christos { "only-verify-crc", no_argument, 0,
1413 1.47 christos OPT_ONLY_VERIFY_CRC },
1414 1.45 christos { "rsh-command", required_argument, 0,
1415 1.47 christos OPT_RSH_COMMAND },
1416 1.45 christos { "version", no_argument, 0,
1417 1.47 christos OPT_VERSION },
1418 1.45 christos #endif
1419 1.83 jmc { 0, 0, 0, 0 },
1420 1.45 christos };
1421 1.45 christos
1422 1.1 jtc /*
1423 1.1 jtc * cpio_options()
1424 1.1 jtc * look at the user specified flags. set globals as required and check if
1425 1.1 jtc * the user specified a legal set of flags. If not, complain and exit
1426 1.1 jtc */
1427 1.1 jtc
1428 1.1 jtc static void
1429 1.7 tls cpio_options(int argc, char **argv)
1430 1.1 jtc {
1431 1.26 itohy FSUB tmp;
1432 1.10 mrg unsigned int flg = 0;
1433 1.10 mrg unsigned int bflg = 0;
1434 1.10 mrg int c, i;
1435 1.40 christos FILE *fp;
1436 1.40 christos char *str;
1437 1.10 mrg
1438 1.40 christos uflag = 1;
1439 1.40 christos kflag = 1;
1440 1.40 christos pids = 1;
1441 1.40 christos pmode = 1;
1442 1.40 christos pmtime = 0;
1443 1.40 christos arcname = NULL;
1444 1.40 christos dflag = 1;
1445 1.40 christos nodirs = 1;
1446 1.10 mrg /*
1447 1.10 mrg * process option flags
1448 1.10 mrg */
1449 1.29 thorpej while ((c = getoldopt(argc, argv,
1450 1.47 christos "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
1451 1.45 christos cpio_longopts, NULL)) != -1) {
1452 1.10 mrg switch(c) {
1453 1.10 mrg case 'a':
1454 1.10 mrg /*
1455 1.10 mrg * preserve access time on filesystem nodes we read
1456 1.10 mrg */
1457 1.10 mrg tflag = 1;
1458 1.10 mrg flg |= TF;
1459 1.10 mrg break;
1460 1.10 mrg #ifdef notyet
1461 1.10 mrg case 'b':
1462 1.40 christos /*
1463 1.40 christos * swap bytes and half-words when reading data
1464 1.40 christos */
1465 1.10 mrg break;
1466 1.10 mrg #endif
1467 1.10 mrg case 'c':
1468 1.40 christos /*
1469 1.40 christos * ASCII cpio header
1470 1.40 christos */
1471 1.10 mrg frmt = &fsub[F_SV4CPIO];
1472 1.10 mrg break;
1473 1.10 mrg case 'd':
1474 1.10 mrg /*
1475 1.40 christos * create directories as needed
1476 1.10 mrg * pax does this by default ..
1477 1.10 mrg */
1478 1.40 christos nodirs = 0;
1479 1.10 mrg break;
1480 1.10 mrg case 'f':
1481 1.10 mrg /*
1482 1.10 mrg * inverse match on patterns
1483 1.10 mrg */
1484 1.10 mrg cflag = 1;
1485 1.10 mrg flg |= CF;
1486 1.10 mrg break;
1487 1.10 mrg case 'i':
1488 1.10 mrg /*
1489 1.10 mrg * read the archive
1490 1.10 mrg */
1491 1.40 christos act = EXTRACT;
1492 1.10 mrg flg |= RF;
1493 1.10 mrg break;
1494 1.10 mrg #ifdef notyet
1495 1.10 mrg case 'k':
1496 1.10 mrg break;
1497 1.10 mrg #endif
1498 1.10 mrg case 'l':
1499 1.10 mrg /*
1500 1.10 mrg * try to link src to dest with copy (-rw)
1501 1.10 mrg */
1502 1.10 mrg lflag = 1;
1503 1.10 mrg flg |= LF;
1504 1.10 mrg break;
1505 1.10 mrg case 'm':
1506 1.10 mrg /*
1507 1.10 mrg * preserve mtime
1508 1.10 mrg */
1509 1.10 mrg flg |= PF;
1510 1.10 mrg pmtime = 1;
1511 1.10 mrg break;
1512 1.10 mrg case 'o':
1513 1.10 mrg /*
1514 1.10 mrg * write an archive
1515 1.10 mrg */
1516 1.40 christos act = ARCHIVE;
1517 1.40 christos frmt = &(fsub[F_SV4CRC]);
1518 1.10 mrg flg |= WF;
1519 1.10 mrg break;
1520 1.10 mrg case 'p':
1521 1.10 mrg /*
1522 1.10 mrg * cpio -p is like pax -rw
1523 1.10 mrg */
1524 1.40 christos act = COPY;
1525 1.10 mrg flg |= RF | WF;
1526 1.10 mrg break;
1527 1.10 mrg case 'r':
1528 1.10 mrg /*
1529 1.10 mrg * interactive file rename
1530 1.10 mrg */
1531 1.10 mrg iflag = 1;
1532 1.10 mrg flg |= IF;
1533 1.10 mrg break;
1534 1.10 mrg #ifdef notyet
1535 1.10 mrg case 's':
1536 1.40 christos /*
1537 1.40 christos * swap bytes after reading data
1538 1.40 christos */
1539 1.10 mrg break;
1540 1.10 mrg #endif
1541 1.10 mrg case 't':
1542 1.40 christos /*
1543 1.40 christos * list contents of archive
1544 1.40 christos */
1545 1.10 mrg act = LIST;
1546 1.40 christos listf = stdout;
1547 1.82 tron flg &= ~RF;
1548 1.10 mrg break;
1549 1.10 mrg case 'u':
1550 1.10 mrg /*
1551 1.10 mrg * don't ignore those older files
1552 1.10 mrg */
1553 1.10 mrg uflag = 0;
1554 1.40 christos kflag = 0;
1555 1.10 mrg flg |= UF;
1556 1.10 mrg break;
1557 1.10 mrg case 'v':
1558 1.10 mrg /*
1559 1.10 mrg * verbose operation mode
1560 1.10 mrg */
1561 1.10 mrg vflag = 1;
1562 1.10 mrg flg |= VF;
1563 1.10 mrg break;
1564 1.40 christos case 'z':
1565 1.40 christos /*
1566 1.40 christos * use gzip. Non standard option.
1567 1.40 christos */
1568 1.40 christos gzip_program = GZIP_CMD;
1569 1.40 christos break;
1570 1.40 christos case 'A':
1571 1.40 christos /*
1572 1.40 christos * append to an archive
1573 1.40 christos */
1574 1.40 christos act = APPND;
1575 1.40 christos flg |= AF;
1576 1.40 christos break;
1577 1.40 christos case 'B':
1578 1.40 christos /*
1579 1.40 christos * set blocksize to 5120
1580 1.40 christos */
1581 1.40 christos blksz = 5120;
1582 1.40 christos break;
1583 1.40 christos case 'C':
1584 1.40 christos /*
1585 1.40 christos * specify blocksize
1586 1.40 christos */
1587 1.40 christos if ((blksz = (int)str_offt(optarg)) <= 0) {
1588 1.40 christos tty_warn(1, "Invalid block size %s", optarg);
1589 1.59 kristerw cpio_usage();
1590 1.40 christos }
1591 1.40 christos break;
1592 1.40 christos case 'E':
1593 1.40 christos /*
1594 1.40 christos * file with patterns to extract or list
1595 1.40 christos */
1596 1.40 christos if ((fp = fopen(optarg, "r")) == NULL) {
1597 1.40 christos tty_warn(1, "Unable to open file '%s' for read",
1598 1.40 christos optarg);
1599 1.40 christos cpio_usage();
1600 1.40 christos }
1601 1.40 christos while ((str = getline(fp)) != NULL) {
1602 1.40 christos pat_add(str, NULL);
1603 1.40 christos }
1604 1.40 christos fclose(fp);
1605 1.40 christos if (getline_error) {
1606 1.40 christos tty_warn(1, "Problem with file '%s'", optarg);
1607 1.40 christos cpio_usage();
1608 1.40 christos }
1609 1.40 christos break;
1610 1.40 christos case 'H':
1611 1.40 christos /*
1612 1.40 christos * specify an archive format on write
1613 1.40 christos */
1614 1.40 christos tmp.name = optarg;
1615 1.40 christos frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1616 1.40 christos sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
1617 1.40 christos if (frmt != NULL) {
1618 1.40 christos flg |= XF;
1619 1.40 christos break;
1620 1.40 christos }
1621 1.40 christos tty_warn(1, "Unknown -H format: %s", optarg);
1622 1.40 christos (void)fputs("cpio: Known -H formats are:", stderr);
1623 1.40 christos for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1624 1.40 christos (void)fprintf(stderr, " %s", fsub[i].name);
1625 1.40 christos (void)fputs("\n\n", stderr);
1626 1.59 kristerw cpio_usage();
1627 1.40 christos break;
1628 1.40 christos case 'I':
1629 1.40 christos case 'O':
1630 1.40 christos /*
1631 1.40 christos * filename where the archive is stored
1632 1.40 christos */
1633 1.40 christos if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1634 1.40 christos /*
1635 1.40 christos * treat a - as stdin
1636 1.40 christos */
1637 1.40 christos arcname = NULL;
1638 1.40 christos break;
1639 1.40 christos }
1640 1.40 christos arcname = optarg;
1641 1.40 christos break;
1642 1.40 christos case 'L':
1643 1.40 christos /*
1644 1.40 christos * follow symlinks
1645 1.40 christos */
1646 1.40 christos Lflag = 1;
1647 1.40 christos flg |= CLF;
1648 1.40 christos break;
1649 1.40 christos #ifdef notyet
1650 1.40 christos case 'M':
1651 1.40 christos arg = optarg;
1652 1.40 christos break;
1653 1.40 christos case 'R':
1654 1.40 christos arg = optarg;
1655 1.40 christos break;
1656 1.40 christos #endif
1657 1.40 christos case 'S':
1658 1.40 christos /*
1659 1.40 christos * swap halfwords after reading data
1660 1.40 christos */
1661 1.40 christos cpio_swp_head = 1;
1662 1.40 christos break;
1663 1.40 christos #ifdef notyet
1664 1.40 christos case 'V':
1665 1.40 christos break;
1666 1.40 christos #endif
1667 1.40 christos case 'Z':
1668 1.40 christos /*
1669 1.40 christos * use compress. Non standard option.
1670 1.40 christos */
1671 1.40 christos gzip_program = COMPRESS_CMD;
1672 1.40 christos break;
1673 1.40 christos case '6':
1674 1.40 christos /*
1675 1.40 christos * process Version 6 cpio format
1676 1.40 christos */
1677 1.40 christos frmt = &(fsub[F_BCPIO]);
1678 1.45 christos case OPT_FORCE_LOCAL:
1679 1.45 christos forcelocal = 1;
1680 1.47 christos break;
1681 1.47 christos case OPT_INSECURE:
1682 1.47 christos secure = 0;
1683 1.45 christos break;
1684 1.78 christos
1685 1.78 christos case OPT_SPARSE:
1686 1.78 christos /* do nothing; we already generate sparse files */
1687 1.78 christos break;
1688 1.10 mrg default:
1689 1.10 mrg cpio_usage();
1690 1.10 mrg break;
1691 1.10 mrg }
1692 1.10 mrg }
1693 1.10 mrg
1694 1.10 mrg /*
1695 1.10 mrg * figure out the operation mode of cpio. check that we have not been
1696 1.10 mrg * given a bogus set of flags for the operation mode.
1697 1.10 mrg */
1698 1.10 mrg if (ISLIST(flg)) {
1699 1.10 mrg act = LIST;
1700 1.10 mrg bflg = flg & BDLIST;
1701 1.10 mrg } else if (ISEXTRACT(flg)) {
1702 1.10 mrg act = EXTRACT;
1703 1.10 mrg bflg = flg & BDEXTR;
1704 1.10 mrg } else if (ISARCHIVE(flg)) {
1705 1.10 mrg act = ARCHIVE;
1706 1.10 mrg bflg = flg & BDARCH;
1707 1.10 mrg } else if (ISAPPND(flg)) {
1708 1.10 mrg act = APPND;
1709 1.10 mrg bflg = flg & BDARCH;
1710 1.10 mrg } else if (ISCOPY(flg)) {
1711 1.10 mrg act = COPY;
1712 1.10 mrg bflg = flg & BDCOPY;
1713 1.10 mrg } else
1714 1.10 mrg cpio_usage();
1715 1.10 mrg if (bflg) {
1716 1.10 mrg cpio_usage();
1717 1.10 mrg }
1718 1.10 mrg
1719 1.10 mrg /*
1720 1.10 mrg * if we are writing (ARCHIVE) we use the default format if the user
1721 1.10 mrg * did not specify a format. when we write during an APPEND, we will
1722 1.10 mrg * adopt the format of the existing archive if none was supplied.
1723 1.10 mrg */
1724 1.10 mrg if (!(flg & XF) && (act == ARCHIVE))
1725 1.10 mrg frmt = &(fsub[F_BCPIO]);
1726 1.10 mrg
1727 1.10 mrg /*
1728 1.10 mrg * process the args as they are interpreted by the operation mode
1729 1.10 mrg */
1730 1.10 mrg switch (act) {
1731 1.10 mrg case LIST:
1732 1.10 mrg case EXTRACT:
1733 1.10 mrg for (; optind < argc; optind++)
1734 1.23 is if (pat_add(argv[optind], 0) < 0)
1735 1.10 mrg cpio_usage();
1736 1.10 mrg break;
1737 1.10 mrg case COPY:
1738 1.10 mrg if (optind >= argc) {
1739 1.10 mrg tty_warn(0, "Destination directory was not supplied");
1740 1.10 mrg cpio_usage();
1741 1.10 mrg }
1742 1.10 mrg --argc;
1743 1.10 mrg dirptr = argv[argc];
1744 1.14 mycroft /* FALLTHROUGH */
1745 1.10 mrg case ARCHIVE:
1746 1.10 mrg case APPND:
1747 1.40 christos if (argc != optind) {
1748 1.40 christos for (; optind < argc; optind++)
1749 1.40 christos if (ftree_add(argv[optind], 0) < 0)
1750 1.40 christos cpio_usage();
1751 1.40 christos break;
1752 1.40 christos }
1753 1.10 mrg /*
1754 1.10 mrg * no read errors allowed on updates/append operation!
1755 1.10 mrg */
1756 1.10 mrg maxflt = 0;
1757 1.40 christos while ((str = getline(stdin)) != NULL) {
1758 1.68 fvdl ftree_add(str, 0);
1759 1.40 christos }
1760 1.40 christos if (getline_error) {
1761 1.40 christos tty_warn(1, "Problem while reading stdin");
1762 1.40 christos cpio_usage();
1763 1.40 christos }
1764 1.40 christos break;
1765 1.40 christos default:
1766 1.40 christos cpio_usage();
1767 1.10 mrg break;
1768 1.10 mrg }
1769 1.1 jtc }
1770 1.89 christos #endif
1771 1.1 jtc
1772 1.1 jtc /*
1773 1.1 jtc * printflg()
1774 1.1 jtc * print out those invalid flag sets found to the user
1775 1.1 jtc */
1776 1.1 jtc
1777 1.1 jtc static void
1778 1.1 jtc printflg(unsigned int flg)
1779 1.1 jtc {
1780 1.1 jtc int nxt;
1781 1.1 jtc
1782 1.1 jtc (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1783 1.8 christos while ((nxt = ffs(flg)) != 0) {
1784 1.70 jmmv flg &= ~(1 << (nxt - 1));
1785 1.70 jmmv (void)fprintf(stderr, " -%c", flgch[nxt - 1]);
1786 1.1 jtc }
1787 1.1 jtc (void)putc('\n', stderr);
1788 1.1 jtc }
1789 1.1 jtc
1790 1.1 jtc /*
1791 1.1 jtc * c_frmt()
1792 1.1 jtc * comparison routine used by bsearch to find the format specified
1793 1.1 jtc * by the user
1794 1.1 jtc */
1795 1.1 jtc
1796 1.1 jtc static int
1797 1.1 jtc c_frmt(const void *a, const void *b)
1798 1.1 jtc {
1799 1.93 dsl return strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name);
1800 1.1 jtc }
1801 1.1 jtc
1802 1.1 jtc /*
1803 1.1 jtc * opt_next()
1804 1.1 jtc * called by format specific options routines to get each format specific
1805 1.1 jtc * flag and value specified with -o
1806 1.1 jtc * Return:
1807 1.1 jtc * pointer to next OPLIST entry or NULL (end of list).
1808 1.1 jtc */
1809 1.1 jtc
1810 1.1 jtc OPLIST *
1811 1.1 jtc opt_next(void)
1812 1.1 jtc {
1813 1.1 jtc OPLIST *opt;
1814 1.1 jtc
1815 1.1 jtc if ((opt = ophead) != NULL)
1816 1.1 jtc ophead = ophead->fow;
1817 1.93 dsl return opt;
1818 1.1 jtc }
1819 1.1 jtc
1820 1.1 jtc /*
1821 1.1 jtc * bad_opt()
1822 1.1 jtc * generic routine used to complain about a format specific options
1823 1.1 jtc * when the format does not support options.
1824 1.1 jtc */
1825 1.1 jtc
1826 1.1 jtc int
1827 1.1 jtc bad_opt(void)
1828 1.1 jtc {
1829 1.7 tls OPLIST *opt;
1830 1.1 jtc
1831 1.1 jtc if (ophead == NULL)
1832 1.93 dsl return 0;
1833 1.1 jtc /*
1834 1.1 jtc * print all we were given
1835 1.1 jtc */
1836 1.48 christos tty_warn(1," These format options are not supported for %s",
1837 1.48 christos frmt->name);
1838 1.1 jtc while ((opt = opt_next()) != NULL)
1839 1.1 jtc (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1840 1.48 christos if (strcmp(NM_TAR, argv0) == 0)
1841 1.48 christos tar_usage();
1842 1.89 christos #ifndef NO_CPIO
1843 1.48 christos else if (strcmp(NM_CPIO, argv0) == 0)
1844 1.48 christos cpio_usage();
1845 1.89 christos #endif
1846 1.48 christos else
1847 1.48 christos pax_usage();
1848 1.93 dsl return 0;
1849 1.1 jtc }
1850 1.1 jtc
1851 1.1 jtc /*
1852 1.1 jtc * opt_add()
1853 1.1 jtc * breaks the value supplied to -o into a option name and value. options
1854 1.1 jtc * are given to -o in the form -o name-value,name=value
1855 1.26 itohy * multiple -o may be specified.
1856 1.1 jtc * Return:
1857 1.1 jtc * 0 if format in name=value format, -1 if -o is passed junk
1858 1.1 jtc */
1859 1.1 jtc
1860 1.1 jtc int
1861 1.18 tv opt_add(const char *str)
1862 1.1 jtc {
1863 1.7 tls OPLIST *opt;
1864 1.7 tls char *frpt;
1865 1.7 tls char *pt;
1866 1.7 tls char *endpt;
1867 1.40 christos char *dstr;
1868 1.1 jtc
1869 1.1 jtc if ((str == NULL) || (*str == '\0')) {
1870 1.8 christos tty_warn(0, "Invalid option name");
1871 1.93 dsl return -1;
1872 1.1 jtc }
1873 1.40 christos if ((dstr = strdup(str)) == NULL) {
1874 1.40 christos tty_warn(0, "Unable to allocate space for option list");
1875 1.93 dsl return -1;
1876 1.40 christos }
1877 1.40 christos frpt = endpt = dstr;
1878 1.1 jtc
1879 1.1 jtc /*
1880 1.1 jtc * break into name and values pieces and stuff each one into a
1881 1.1 jtc * OPLIST structure. When we know the format, the format specific
1882 1.1 jtc * option function will go through this list
1883 1.1 jtc */
1884 1.1 jtc while ((frpt != NULL) && (*frpt != '\0')) {
1885 1.1 jtc if ((endpt = strchr(frpt, ',')) != NULL)
1886 1.1 jtc *endpt = '\0';
1887 1.1 jtc if ((pt = strchr(frpt, '=')) == NULL) {
1888 1.8 christos tty_warn(0, "Invalid options format");
1889 1.40 christos free(dstr);
1890 1.93 dsl return -1;
1891 1.1 jtc }
1892 1.1 jtc if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1893 1.8 christos tty_warn(0, "Unable to allocate space for option list");
1894 1.40 christos free(dstr);
1895 1.93 dsl return -1;
1896 1.1 jtc }
1897 1.1 jtc *pt++ = '\0';
1898 1.1 jtc opt->name = frpt;
1899 1.1 jtc opt->value = pt;
1900 1.1 jtc opt->fow = NULL;
1901 1.1 jtc if (endpt != NULL)
1902 1.1 jtc frpt = endpt + 1;
1903 1.1 jtc else
1904 1.1 jtc frpt = NULL;
1905 1.1 jtc if (ophead == NULL) {
1906 1.1 jtc optail = ophead = opt;
1907 1.1 jtc continue;
1908 1.1 jtc }
1909 1.1 jtc optail->fow = opt;
1910 1.1 jtc optail = opt;
1911 1.1 jtc }
1912 1.93 dsl return 0;
1913 1.1 jtc }
1914 1.1 jtc
1915 1.1 jtc /*
1916 1.1 jtc * str_offt()
1917 1.1 jtc * Convert an expression of the following forms to an off_t > 0.
1918 1.26 itohy * 1) A positive decimal number.
1919 1.1 jtc * 2) A positive decimal number followed by a b (mult by 512).
1920 1.1 jtc * 3) A positive decimal number followed by a k (mult by 1024).
1921 1.1 jtc * 4) A positive decimal number followed by a m (mult by 512).
1922 1.1 jtc * 5) A positive decimal number followed by a w (mult by sizeof int)
1923 1.1 jtc * 6) Two or more positive decimal numbers (with/without k,b or w).
1924 1.32 wiz * separated by x (also * for backwards compatibility), specifying
1925 1.1 jtc * the product of the indicated values.
1926 1.1 jtc * Return:
1927 1.1 jtc * 0 for an error, a positive value o.w.
1928 1.1 jtc */
1929 1.1 jtc
1930 1.1 jtc static off_t
1931 1.1 jtc str_offt(char *val)
1932 1.1 jtc {
1933 1.1 jtc char *expr;
1934 1.1 jtc off_t num, t;
1935 1.1 jtc
1936 1.33 lukem num = STRTOOFFT(val, &expr, 0);
1937 1.33 lukem if ((num == OFFT_MAX) || (num <= 0) || (expr == val))
1938 1.93 dsl return 0;
1939 1.1 jtc
1940 1.1 jtc switch(*expr) {
1941 1.1 jtc case 'b':
1942 1.1 jtc t = num;
1943 1.1 jtc num *= 512;
1944 1.1 jtc if (t > num)
1945 1.93 dsl return 0;
1946 1.1 jtc ++expr;
1947 1.1 jtc break;
1948 1.1 jtc case 'k':
1949 1.1 jtc t = num;
1950 1.1 jtc num *= 1024;
1951 1.1 jtc if (t > num)
1952 1.93 dsl return 0;
1953 1.1 jtc ++expr;
1954 1.1 jtc break;
1955 1.1 jtc case 'm':
1956 1.1 jtc t = num;
1957 1.1 jtc num *= 1048576;
1958 1.1 jtc if (t > num)
1959 1.93 dsl return 0;
1960 1.1 jtc ++expr;
1961 1.1 jtc break;
1962 1.1 jtc case 'w':
1963 1.1 jtc t = num;
1964 1.1 jtc num *= sizeof(int);
1965 1.1 jtc if (t > num)
1966 1.93 dsl return 0;
1967 1.1 jtc ++expr;
1968 1.1 jtc break;
1969 1.1 jtc }
1970 1.1 jtc
1971 1.1 jtc switch(*expr) {
1972 1.1 jtc case '\0':
1973 1.1 jtc break;
1974 1.1 jtc case '*':
1975 1.1 jtc case 'x':
1976 1.1 jtc t = num;
1977 1.1 jtc num *= str_offt(expr + 1);
1978 1.1 jtc if (t > num)
1979 1.93 dsl return 0;
1980 1.1 jtc break;
1981 1.1 jtc default:
1982 1.93 dsl return 0;
1983 1.1 jtc }
1984 1.93 dsl return num;
1985 1.1 jtc }
1986 1.1 jtc
1987 1.40 christos char *
1988 1.40 christos getline(FILE *f)
1989 1.40 christos {
1990 1.40 christos char *name, *temp;
1991 1.40 christos size_t len;
1992 1.40 christos
1993 1.40 christos name = fgetln(f, &len);
1994 1.40 christos if (!name) {
1995 1.40 christos getline_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
1996 1.93 dsl return 0;
1997 1.40 christos }
1998 1.40 christos if (name[len-1] != '\n')
1999 1.40 christos len++;
2000 1.40 christos temp = malloc(len);
2001 1.40 christos if (!temp) {
2002 1.40 christos getline_error = GETLINE_OUT_OF_MEM;
2003 1.93 dsl return 0;
2004 1.40 christos }
2005 1.40 christos memcpy(temp, name, len-1);
2006 1.40 christos temp[len-1] = 0;
2007 1.93 dsl return temp;
2008 1.40 christos }
2009 1.40 christos
2010 1.1 jtc /*
2011 1.1 jtc * no_op()
2012 1.1 jtc * for those option functions where the archive format has nothing to do.
2013 1.1 jtc * Return:
2014 1.1 jtc * 0
2015 1.1 jtc */
2016 1.1 jtc
2017 1.1 jtc static int
2018 1.1 jtc no_op(void)
2019 1.1 jtc {
2020 1.93 dsl return 0;
2021 1.1 jtc }
2022 1.1 jtc
2023 1.1 jtc /*
2024 1.1 jtc * pax_usage()
2025 1.1 jtc * print the usage summary to the user
2026 1.1 jtc */
2027 1.1 jtc
2028 1.1 jtc void
2029 1.1 jtc pax_usage(void)
2030 1.1 jtc {
2031 1.35 lukem fprintf(stderr,
2032 1.71 jmmv "usage: pax [-cdjnvzO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
2033 1.35 lukem " [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
2034 1.35 lukem " [pattern ...]\n");
2035 1.35 lukem fprintf(stderr,
2036 1.63 christos " pax -r [-cdijknuvzADOYZ] [-E limit] [-f archive] [-N dbdir]\n"
2037 1.35 lukem " [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
2038 1.35 lukem " [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
2039 1.35 lukem fprintf(stderr,
2040 1.63 christos " pax -w [-dijtuvzAHLMOPX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
2041 1.35 lukem " [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
2042 1.35 lukem " [-U user] ... [-G group] ...\n"
2043 1.35 lukem " [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
2044 1.35 lukem fprintf(stderr,
2045 1.63 christos " pax -r -w [-dijklntuvzADHLMOPXYZ] [-N dbdir] [-p string] ...\n"
2046 1.35 lukem " [-s replstr] ... [-U user] ... [-G group] ...\n"
2047 1.35 lukem " [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
2048 1.1 jtc exit(1);
2049 1.12 mycroft /* NOTREACHED */
2050 1.1 jtc }
2051 1.1 jtc
2052 1.1 jtc /*
2053 1.1 jtc * tar_usage()
2054 1.1 jtc * print the usage summary to the user
2055 1.1 jtc */
2056 1.1 jtc
2057 1.1 jtc void
2058 1.1 jtc tar_usage(void)
2059 1.1 jtc {
2060 1.80 dsl (void)fputs("usage: tar [-]{crtux}[-befhjlmopqvwzHOPSXZ014578] [archive] "
2061 1.44 soren "[blocksize]\n"
2062 1.44 soren " [-C directory] [-T file] [-s replstr] "
2063 1.44 soren "[file ...]\n", stderr);
2064 1.1 jtc exit(1);
2065 1.12 mycroft /* NOTREACHED */
2066 1.1 jtc }
2067 1.1 jtc
2068 1.89 christos #ifndef NO_CPIO
2069 1.1 jtc /*
2070 1.1 jtc * cpio_usage()
2071 1.1 jtc * print the usage summary to the user
2072 1.1 jtc */
2073 1.1 jtc
2074 1.1 jtc void
2075 1.1 jtc cpio_usage(void)
2076 1.1 jtc {
2077 1.10 mrg
2078 1.71 jmmv (void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
2079 1.51 soren "[-H format] [-O archive]\n"
2080 1.51 soren " < name-list [> archive]\n"
2081 1.51 soren " cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
2082 1.51 soren "[-F archive] [-H format] \n"
2083 1.51 soren " [-I archive] "
2084 1.51 soren "[pattern ...] [< archive]\n"
2085 1.51 soren " cpio -p [-adlLmuv] destination-directory "
2086 1.51 soren "< name-list\n", stderr);
2087 1.1 jtc exit(1);
2088 1.12 mycroft /* NOTREACHED */
2089 1.1 jtc }
2090 1.89 christos #endif
2091