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