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