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