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