options.c revision 1.10 1 /* $NetBSD: options.c,v 1.10 1998/03/06 09:13:02 mrg 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.10 1998/03/06 09:13:02 mrg 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 return(tar_options(argc, argv));
173 else if (strcmp(NM_CPIO, argv0) == 0)
174 return(cpio_options(argc, argv));
175 /*
176 * assume pax as the default
177 */
178 argv0 = NM_PAX;
179 return(pax_options(argc, argv));
180 }
181
182 /*
183 * pax_options()
184 * look at the user specified flags. set globals as required and check if
185 * the user specified a legal set of flags. If not, complain and exit
186 */
187
188 #if __STDC__
189 static void
190 pax_options(int argc, char **argv)
191 #else
192 static void
193 pax_options(argc, argv)
194 int argc;
195 char **argv;
196 #endif
197 {
198 int c;
199 int i;
200 unsigned int flg = 0;
201 unsigned int bflg = 0;
202 char *pt;
203 FSUB tmp;
204 extern char *optarg;
205 extern int optind;
206
207 /*
208 * process option flags
209 */
210 while ((c=getopt(argc,argv,"ab:cdf:iklno:p:rs:tuvwx:zB:DE:G:HLPT:U:XYZ"))
211 != -1) {
212 switch (c) {
213 case 'a':
214 /*
215 * append
216 */
217 flg |= AF;
218 break;
219 case 'b':
220 /*
221 * specify blocksize
222 */
223 flg |= BF;
224 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
225 tty_warn(1, "Invalid block size %s", optarg);
226 pax_usage();
227 }
228 break;
229 case 'c':
230 /*
231 * inverse match on patterns
232 */
233 cflag = 1;
234 flg |= CF;
235 break;
236 case 'd':
237 /*
238 * match only dir on extract, not the subtree at dir
239 */
240 dflag = 1;
241 flg |= DF;
242 break;
243 case 'f':
244 /*
245 * filename where the archive is stored
246 */
247 arcname = optarg;
248 flg |= FF;
249 break;
250 case 'i':
251 /*
252 * interactive file rename
253 */
254 iflag = 1;
255 flg |= IF;
256 break;
257 case 'k':
258 /*
259 * do not clobber files that exist
260 */
261 kflag = 1;
262 flg |= KF;
263 break;
264 case 'l':
265 /*
266 * try to link src to dest with copy (-rw)
267 */
268 lflag = 1;
269 flg |= LF;
270 break;
271 case 'n':
272 /*
273 * select first match for a pattern only
274 */
275 nflag = 1;
276 flg |= NF;
277 break;
278 case 'o':
279 /*
280 * pass format specific options
281 */
282 flg |= OF;
283 if (opt_add(optarg) < 0)
284 pax_usage();
285 break;
286 case 'p':
287 /*
288 * specify file characteristic options
289 */
290 for (pt = optarg; *pt != '\0'; ++pt) {
291 switch(*pt) {
292 case 'a':
293 /*
294 * do not preserve access time
295 */
296 patime = 0;
297 break;
298 case 'e':
299 /*
300 * preserve user id, group id, file
301 * mode, access/modification times
302 */
303 pids = 1;
304 pmode = 1;
305 patime = 1;
306 pmtime = 1;
307 break;
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 * preserver 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 'B':
404 /*
405 * non-standard option on number of bytes written on a
406 * single archive volume.
407 */
408 if ((wrlimit = str_offt(optarg)) <= 0) {
409 tty_warn(1, "Invalid write limit %s", optarg);
410 pax_usage();
411 }
412 if (wrlimit % BLKMULT) {
413 tty_warn(1,
414 "Write limit is not a %d byte multiple",
415 BLKMULT);
416 pax_usage();
417 }
418 flg |= CBF;
419 break;
420 case 'D':
421 /*
422 * On extraction check file inode change time before the
423 * modification of the file name. Non standard option.
424 */
425 Dflag = 1;
426 flg |= CDF;
427 break;
428 case 'E':
429 /*
430 * non-standard limit on read faults
431 * 0 indicates stop after first error, values
432 * indicate a limit, "NONE" try forever
433 */
434 flg |= CEF;
435 if (strcmp(NONE, optarg) == 0)
436 maxflt = -1;
437 else if ((maxflt = atoi(optarg)) < 0) {
438 tty_warn(1,
439 "Error count value must be positive");
440 pax_usage();
441 }
442 break;
443 case 'G':
444 /*
445 * non-standard option for selecting files within an
446 * archive by group (gid or name)
447 */
448 if (grp_add(optarg) < 0) {
449 pax_usage();
450 break;
451 }
452 flg |= CGF;
453 break;
454 case 'H':
455 /*
456 * follow command line symlinks only
457 */
458 Hflag = 1;
459 flg |= CHF;
460 break;
461 case 'L':
462 /*
463 * follow symlinks
464 */
465 Lflag = 1;
466 flg |= CLF;
467 break;
468 case 'P':
469 /*
470 * do NOT follow symlinks (default)
471 */
472 Lflag = 0;
473 flg |= CPF;
474 break;
475 case 'T':
476 /*
477 * non-standard option for selecting files within an
478 * archive by modification time range (lower,upper)
479 */
480 if (trng_add(optarg) < 0) {
481 pax_usage();
482 break;
483 }
484 flg |= CTF;
485 break;
486 case 'U':
487 /*
488 * non-standard option for selecting files within an
489 * archive by user (uid or name)
490 */
491 if (usr_add(optarg) < 0) {
492 pax_usage();
493 break;
494 }
495 flg |= CUF;
496 break;
497 case 'X':
498 /*
499 * do not pass over mount points in the file system
500 */
501 Xflag = 1;
502 flg |= CXF;
503 break;
504 case 'Y':
505 /*
506 * On extraction check file inode change time after the
507 * modification of the file name. Non standard option.
508 */
509 Yflag = 1;
510 flg |= CYF;
511 break;
512 case 'Z':
513 /*
514 * On extraction check modification time after the
515 * modification of the file name. Non standard option.
516 */
517 Zflag = 1;
518 flg |= CZF;
519 break;
520 case '?':
521 default:
522 pax_usage();
523 break;
524 }
525 }
526
527 /*
528 * figure out the operation mode of pax read,write,extract,copy,append
529 * or list. check that we have not been given a bogus set of flags
530 * for the operation mode.
531 */
532 if (ISLIST(flg)) {
533 act = LIST;
534 bflg = flg & BDLIST;
535 } else if (ISEXTRACT(flg)) {
536 act = EXTRACT;
537 bflg = flg & BDEXTR;
538 } else if (ISARCHIVE(flg)) {
539 act = ARCHIVE;
540 bflg = flg & BDARCH;
541 } else if (ISAPPND(flg)) {
542 act = APPND;
543 bflg = flg & BDARCH;
544 } else if (ISCOPY(flg)) {
545 act = COPY;
546 bflg = flg & BDCOPY;
547 } else
548 pax_usage();
549 if (bflg) {
550 printflg(flg);
551 pax_usage();
552 }
553
554 /*
555 * if we are writing (ARCHIVE) we use the default format if the user
556 * did not specify a format. when we write during an APPEND, we will
557 * adopt the format of the existing archive if none was supplied.
558 */
559 if (!(flg & XF) && (act == ARCHIVE))
560 frmt = &(fsub[DEFLT]);
561
562 /*
563 * process the args as they are interpreted by the operation mode
564 */
565 switch (act) {
566 case LIST:
567 case EXTRACT:
568 for (; optind < argc; optind++)
569 if (pat_add(argv[optind]) < 0)
570 pax_usage();
571 break;
572 case COPY:
573 if (optind >= argc) {
574 tty_warn(0, "Destination directory was not supplied");
575 pax_usage();
576 }
577 --argc;
578 dirptr = argv[argc];
579 /* FALL THROUGH */
580 case ARCHIVE:
581 case APPND:
582 for (; optind < argc; optind++)
583 if (ftree_add(argv[optind]) < 0)
584 pax_usage();
585 /*
586 * no read errors allowed on updates/append operation!
587 */
588 maxflt = 0;
589 break;
590 }
591 }
592
593
594 /*
595 * tar_options()
596 * look at the user specified flags. set globals as required and check if
597 * the user specified a legal set of flags. If not, complain and exit
598 */
599
600 #if __STDC__
601 static void
602 tar_options(int argc, char **argv)
603 #else
604 static void
605 tar_options(argc, argv)
606 int argc;
607 char **argv;
608 #endif
609 {
610 int c;
611 int fstdin = 0;
612
613 /*
614 * process option flags
615 */
616 while ((c = getoldopt(argc, argv, "b:cef:lmoprutvwxzBC:HLPXZ014578"))
617 != -1) {
618 switch(c) {
619 case 'b':
620 /*
621 * specify blocksize
622 */
623 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
624 tty_warn(1, "Invalid block size %s", optarg);
625 tar_usage();
626 }
627 break;
628 case 'c':
629 /*
630 * create an archive
631 */
632 act = ARCHIVE;
633 break;
634 case 'C':
635 /*
636 * chdir here before extracting.
637 */
638 chdir_dir = optarg;
639 break;
640 case 'e':
641 /*
642 * stop after first error
643 */
644 maxflt = 0;
645 break;
646 case 'f':
647 /*
648 * filename where the archive is stored
649 */
650 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
651 /*
652 * treat a - as stdin
653 */
654 fstdin = 1;
655 arcname = (char *)0;
656 break;
657 }
658 fstdin = 0;
659 arcname = optarg;
660 break;
661 case 'l':
662 /*
663 * do not pass over mount points in the file system
664 */
665 Xflag = 1;
666 break;
667 case 'm':
668 /*
669 * do not preserve modification time
670 */
671 pmtime = 0;
672 break;
673 case 'o':
674 if (opt_add("write_opt=nodir") < 0)
675 tar_usage();
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 * do not pass over mount points in the file system
751 */
752 Xflag = 1;
753 break;
754 case 'Z':
755 /*
756 * use compress.
757 */
758 zflag = 1;
759 gzip_program = COMPRESS_CMD;
760 break;
761 case '0':
762 arcname = DEV_0;
763 break;
764 case '1':
765 arcname = DEV_1;
766 break;
767 case '4':
768 arcname = DEV_4;
769 break;
770 case '5':
771 arcname = DEV_5;
772 break;
773 case '7':
774 arcname = DEV_7;
775 break;
776 case '8':
777 arcname = DEV_8;
778 break;
779 default:
780 tar_usage();
781 break;
782 }
783 }
784 argc -= optind;
785 argv += optind;
786
787 /*
788 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
789 */
790 if (act == ARCHIVE)
791 frmt = &(fsub[F_TAR]);
792
793 /*
794 * process the args as they are interpreted by the operation mode
795 */
796 switch (act) {
797 case LIST:
798 case EXTRACT:
799 default:
800 while (*argv != (char *)NULL)
801 if (pat_add(*argv++) < 0)
802 tar_usage();
803 break;
804 case ARCHIVE:
805 case APPND:
806 while (*argv != (char *)NULL)
807 if (ftree_add(*argv++) < 0)
808 tar_usage();
809 /*
810 * no read errors allowed on updates/append operation!
811 */
812 maxflt = 0;
813 break;
814 }
815 if (!fstdin && ((arcname == (char *)NULL) || (*arcname == '\0'))) {
816 arcname = getenv("TAPE");
817 if ((arcname == (char *)NULL) || (*arcname == '\0'))
818 arcname = DEV_8;
819 }
820 }
821
822 /*
823 * cpio_options()
824 * look at the user specified flags. set globals as required and check if
825 * the user specified a legal set of flags. If not, complain and exit
826 */
827
828 #if __STDC__
829 static void
830 cpio_options(int argc, char **argv)
831 #else
832 static void
833 cpio_options(argc, argv)
834 int argc;
835 char **argv;
836 #endif
837 {
838 FSUB tmp;
839 unsigned int flg = 0;
840 unsigned int bflg = 0;
841 int c, i;
842
843 cpio_mode = uflag = 1;
844 /*
845 * process option flags
846 */
847 while ((c = getoldopt(argc, argv, "ABC:E:H:I:LM:O:R:SVabcdfiklmoprstuv"))
848 != -1) {
849 switch(c) {
850 case 'A':
851 /*
852 * append to an archive
853 */
854 act = APPND;
855 flg |= AF;
856 break;
857 case 'B':
858 /*
859 * set blocksize to 5120
860 */
861 blksz = 5120;
862 break;
863 case 'C':
864 /*
865 * specify blocksize
866 */
867 if ((blksz = (int)str_offt(optarg)) <= 0) {
868 tty_warn(1, "Invalid block size %s", optarg);
869 tar_usage();
870 }
871 break;
872 #ifdef notyet
873 case 'E':
874 arg = optarg;
875 break;
876 #endif
877 case 'H':
878 /*
879 * specify an archive format on write
880 */
881 tmp.name = optarg;
882 frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
883 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
884 if (frmt != NULL) {
885 flg |= XF;
886 break;
887 }
888 tty_warn(1, "Unknown -H format: %s", optarg);
889 (void)fputs("cpio: Known -H formats are:", stderr);
890 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
891 (void)fprintf(stderr, " %s", fsub[i].name);
892 (void)fputs("\n\n", stderr);
893 tar_usage();
894 break;
895 case 'I':
896 case 'O':
897 /*
898 * filename where the archive is stored
899 */
900 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
901 /*
902 * treat a - as stdin
903 */
904 arcname = (char *)0;
905 break;
906 }
907 arcname = optarg;
908 break;
909 case 'L':
910 /*
911 * follow symlinks
912 */
913 Lflag = 1;
914 flg |= CLF;
915 break;
916 #ifdef notyet
917 case 'M':
918 arg = optarg;
919 break;
920 case 'R':
921 arg = optarg;
922 break;
923 #endif
924 case 'S':
925 cpio_swp_head = 1;
926 break;
927 #ifdef notyet
928 case 'V':
929 break;
930 #endif
931 case 'a':
932 /*
933 * preserve access time on filesystem nodes we read
934 */
935 tflag = 1;
936 flg |= TF;
937 break;
938 #ifdef notyet
939 case 'b':
940 break;
941 #endif
942 case 'c':
943 frmt = &fsub[F_SV4CPIO];
944 break;
945 case 'd':
946 /*
947 * pax does this by default ..
948 */
949 flg |= RF;
950 break;
951 case 'f':
952 /*
953 * inverse match on patterns
954 */
955 cflag = 1;
956 flg |= CF;
957 break;
958 case 'i':
959 /*
960 * read the archive
961 */
962 flg |= RF;
963 break;
964 #ifdef notyet
965 case 'k':
966 break;
967 #endif
968 case 'l':
969 /*
970 * try to link src to dest with copy (-rw)
971 */
972 lflag = 1;
973 flg |= LF;
974 break;
975 case 'm':
976 /*
977 * preserve mtime
978 */
979 flg |= PF;
980 pmtime = 1;
981 break;
982 case 'o':
983 /*
984 * write an archive
985 */
986 flg |= WF;
987 break;
988 case 'p':
989 /*
990 * cpio -p is like pax -rw
991 */
992 flg |= RF | WF;
993 break;
994 case 'r':
995 /*
996 * interactive file rename
997 */
998 iflag = 1;
999 flg |= IF;
1000 break;
1001 #ifdef notyet
1002 case 's':
1003 break;
1004 #endif
1005 case 't':
1006 act = LIST;
1007 break;
1008 case 'u':
1009 /*
1010 * don't ignore those older files
1011 */
1012 uflag = 0;
1013 flg |= UF;
1014 break;
1015 case 'v':
1016 /*
1017 * verbose operation mode
1018 */
1019 vflag = 1;
1020 flg |= VF;
1021 break;
1022 default:
1023 cpio_usage();
1024 break;
1025 }
1026 }
1027 argc -= optind;
1028 argv += optind;
1029
1030 /*
1031 * figure out the operation mode of cpio. check that we have not been
1032 * given a bogus set of flags for the operation mode.
1033 */
1034 if (ISLIST(flg)) {
1035 act = LIST;
1036 bflg = flg & BDLIST;
1037 } else if (ISEXTRACT(flg)) {
1038 act = EXTRACT;
1039 bflg = flg & BDEXTR;
1040 } else if (ISARCHIVE(flg)) {
1041 act = ARCHIVE;
1042 bflg = flg & BDARCH;
1043 } else if (ISAPPND(flg)) {
1044 act = APPND;
1045 bflg = flg & BDARCH;
1046 } else if (ISCOPY(flg)) {
1047 act = COPY;
1048 bflg = flg & BDCOPY;
1049 } else
1050 cpio_usage();
1051 if (bflg) {
1052 cpio_usage();
1053 }
1054
1055 /*
1056 * if we are writing (ARCHIVE) we use the default format if the user
1057 * did not specify a format. when we write during an APPEND, we will
1058 * adopt the format of the existing archive if none was supplied.
1059 */
1060 if (!(flg & XF) && (act == ARCHIVE))
1061 frmt = &(fsub[F_BCPIO]);
1062
1063 /*
1064 * process the args as they are interpreted by the operation mode
1065 */
1066 switch (act) {
1067 case LIST:
1068 case EXTRACT:
1069 for (; optind < argc; optind++)
1070 if (pat_add(argv[optind]) < 0)
1071 cpio_usage();
1072 break;
1073 case COPY:
1074 if (optind >= argc) {
1075 tty_warn(0, "Destination directory was not supplied");
1076 cpio_usage();
1077 }
1078 --argc;
1079 dirptr = argv[argc];
1080 /* FALL THROUGH */
1081 case ARCHIVE:
1082 case APPND:
1083 for (; optind < argc; optind++)
1084 if (ftree_add(argv[optind]) < 0)
1085 cpio_usage();
1086 /*
1087 * no read errors allowed on updates/append operation!
1088 */
1089 maxflt = 0;
1090 break;
1091 }
1092 }
1093
1094 /*
1095 * printflg()
1096 * print out those invalid flag sets found to the user
1097 */
1098
1099 #if __STDC__
1100 static void
1101 printflg(unsigned int flg)
1102 #else
1103 static void
1104 printflg(flg)
1105 unsigned int flg;
1106 #endif
1107 {
1108 int nxt;
1109 int pos = 0;
1110
1111 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1112 while ((nxt = ffs(flg)) != 0) {
1113 flg = flg >> nxt;
1114 pos += nxt;
1115 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1116 }
1117 (void)putc('\n', stderr);
1118 }
1119
1120 /*
1121 * c_frmt()
1122 * comparison routine used by bsearch to find the format specified
1123 * by the user
1124 */
1125
1126 #if __STDC__
1127 static int
1128 c_frmt(const void *a, const void *b)
1129 #else
1130 static int
1131 c_frmt(a, b)
1132 void *a;
1133 void *b;
1134 #endif
1135 {
1136 return(strcmp(((FSUB *)a)->name, ((FSUB *)b)->name));
1137 }
1138
1139 /*
1140 * opt_next()
1141 * called by format specific options routines to get each format specific
1142 * flag and value specified with -o
1143 * Return:
1144 * pointer to next OPLIST entry or NULL (end of list).
1145 */
1146
1147 #if __STDC__
1148 OPLIST *
1149 opt_next(void)
1150 #else
1151 OPLIST *
1152 opt_next()
1153 #endif
1154 {
1155 OPLIST *opt;
1156
1157 if ((opt = ophead) != NULL)
1158 ophead = ophead->fow;
1159 return(opt);
1160 }
1161
1162 /*
1163 * bad_opt()
1164 * generic routine used to complain about a format specific options
1165 * when the format does not support options.
1166 */
1167
1168 #if __STDC__
1169 int
1170 bad_opt(void)
1171 #else
1172 int
1173 bad_opt()
1174 #endif
1175 {
1176 OPLIST *opt;
1177
1178 if (ophead == NULL)
1179 return(0);
1180 /*
1181 * print all we were given
1182 */
1183 tty_warn(1,"These format options are not supported");
1184 while ((opt = opt_next()) != NULL)
1185 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1186 pax_usage();
1187 return(0);
1188 }
1189
1190 /*
1191 * opt_add()
1192 * breaks the value supplied to -o into a option name and value. options
1193 * are given to -o in the form -o name-value,name=value
1194 * mulltiple -o may be specified.
1195 * Return:
1196 * 0 if format in name=value format, -1 if -o is passed junk
1197 */
1198
1199 #if __STDC__
1200 int
1201 opt_add(char *str)
1202 #else
1203 int
1204 opt_add(str)
1205 char *str;
1206 #endif
1207 {
1208 OPLIST *opt;
1209 char *frpt;
1210 char *pt;
1211 char *endpt;
1212
1213 if ((str == NULL) || (*str == '\0')) {
1214 tty_warn(0, "Invalid option name");
1215 return(-1);
1216 }
1217 frpt = endpt = str;
1218
1219 /*
1220 * break into name and values pieces and stuff each one into a
1221 * OPLIST structure. When we know the format, the format specific
1222 * option function will go through this list
1223 */
1224 while ((frpt != NULL) && (*frpt != '\0')) {
1225 if ((endpt = strchr(frpt, ',')) != NULL)
1226 *endpt = '\0';
1227 if ((pt = strchr(frpt, '=')) == NULL) {
1228 tty_warn(0, "Invalid options format");
1229 return(-1);
1230 }
1231 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1232 tty_warn(0, "Unable to allocate space for option list");
1233 return(-1);
1234 }
1235 *pt++ = '\0';
1236 opt->name = frpt;
1237 opt->value = pt;
1238 opt->fow = NULL;
1239 if (endpt != NULL)
1240 frpt = endpt + 1;
1241 else
1242 frpt = NULL;
1243 if (ophead == NULL) {
1244 optail = ophead = opt;
1245 continue;
1246 }
1247 optail->fow = opt;
1248 optail = opt;
1249 }
1250 return(0);
1251 }
1252
1253 /*
1254 * str_offt()
1255 * Convert an expression of the following forms to an off_t > 0.
1256 * 1) A positive decimal number.
1257 * 2) A positive decimal number followed by a b (mult by 512).
1258 * 3) A positive decimal number followed by a k (mult by 1024).
1259 * 4) A positive decimal number followed by a m (mult by 512).
1260 * 5) A positive decimal number followed by a w (mult by sizeof int)
1261 * 6) Two or more positive decimal numbers (with/without k,b or w).
1262 * seperated by x (also * for backwards compatibility), specifying
1263 * the product of the indicated values.
1264 * Return:
1265 * 0 for an error, a positive value o.w.
1266 */
1267
1268 #if __STDC__
1269 static off_t
1270 str_offt(char *val)
1271 #else
1272 static off_t
1273 str_offt(val)
1274 char *val;
1275 #endif
1276 {
1277 char *expr;
1278 off_t num, t;
1279
1280 # ifdef NET2_STAT
1281 num = strtol(val, &expr, 0);
1282 if ((num == LONG_MAX) || (num <= 0) || (expr == val))
1283 # else
1284 num = strtoq(val, &expr, 0);
1285 if ((num == QUAD_MAX) || (num <= 0) || (expr == val))
1286 # endif
1287 return(0);
1288
1289 switch(*expr) {
1290 case 'b':
1291 t = num;
1292 num *= 512;
1293 if (t > num)
1294 return(0);
1295 ++expr;
1296 break;
1297 case 'k':
1298 t = num;
1299 num *= 1024;
1300 if (t > num)
1301 return(0);
1302 ++expr;
1303 break;
1304 case 'm':
1305 t = num;
1306 num *= 1048576;
1307 if (t > num)
1308 return(0);
1309 ++expr;
1310 break;
1311 case 'w':
1312 t = num;
1313 num *= sizeof(int);
1314 if (t > num)
1315 return(0);
1316 ++expr;
1317 break;
1318 }
1319
1320 switch(*expr) {
1321 case '\0':
1322 break;
1323 case '*':
1324 case 'x':
1325 t = num;
1326 num *= str_offt(expr + 1);
1327 if (t > num)
1328 return(0);
1329 break;
1330 default:
1331 return(0);
1332 }
1333 return(num);
1334 }
1335
1336 /*
1337 * no_op()
1338 * for those option functions where the archive format has nothing to do.
1339 * Return:
1340 * 0
1341 */
1342
1343 #if __STDC__
1344 static int
1345 no_op(void)
1346 #else
1347 static int
1348 no_op()
1349 #endif
1350 {
1351 return(0);
1352 }
1353
1354 /*
1355 * pax_usage()
1356 * print the usage summary to the user
1357 */
1358
1359 #if __STDC__
1360 void
1361 pax_usage(void)
1362 #else
1363 void
1364 pax_usage()
1365 #endif
1366 {
1367 (void)fputs("usage: pax [-cdnv] [-E limit] [-f archive] ", stderr);
1368 (void)fputs("[-s replstr] ... [-U user] ...", stderr);
1369 (void)fputs("\n [-G group] ... ", stderr);
1370 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1371 (void)fputs("[pattern ...]\n", stderr);
1372 (void)fputs(" pax -r [-cdiknuvDYZ] [-E limit] ", stderr);
1373 (void)fputs("[-f archive] [-o options] ... \n", stderr);
1374 (void)fputs(" [-p string] ... [-s replstr] ... ", stderr);
1375 (void)fputs("[-U user] ... [-G group] ...\n ", stderr);
1376 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1377 (void)fputs(" [pattern ...]\n", stderr);
1378 (void)fputs(" pax -w [-dituvHLPX] [-b blocksize] ", stderr);
1379 (void)fputs("[ [-a] [-f archive] ] [-x format] \n", stderr);
1380 (void)fputs(" [-B bytes] [-s replstr] ... ", stderr);
1381 (void)fputs("[-o options] ... [-U user] ...", stderr);
1382 (void)fputs("\n [-G group] ... ", stderr);
1383 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1384 (void)fputs("[file ...]\n", stderr);
1385 (void)fputs(" pax -r -w [-diklntuvDHLPXYZ] ", stderr);
1386 (void)fputs("[-p string] ... [-s replstr] ...", stderr);
1387 (void)fputs("\n [-U user] ... [-G group] ... ", stderr);
1388 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1389 (void)fputs("\n [file ...] directory\n", stderr);
1390 exit(1);
1391 }
1392
1393 /*
1394 * tar_usage()
1395 * print the usage summary to the user
1396 */
1397
1398 #if __STDC__
1399 void
1400 tar_usage(void)
1401 #else
1402 void
1403 tar_usage()
1404 #endif
1405 {
1406 (void)fputs("usage: tar -{txru}[cevfblmopwBHLPX014578] [tapefile] ",
1407 stderr);
1408 (void)fputs("[blocksize] file1 file2...\n", stderr);
1409 exit(1);
1410 }
1411
1412 /*
1413 * cpio_usage()
1414 * print the usage summary to the user
1415 */
1416
1417 #if __STDC__
1418 void
1419 cpio_usage(void)
1420 #else
1421 void
1422 cpio_usage()
1423 #endif
1424 {
1425
1426 #if 1
1427 (void)fputs(
1428 "usage: cpio -i [-BcdfmrStuv] [ -C blksize ] [ -H header ]\n",
1429 stderr);
1430 (void)fputs(" [ -I file ] [ pattern ... ]\n", stderr);
1431 (void)fputs("usage: cpio -o [-aABcLv] [ -C bufsize ] [ -H header ]\n",
1432 stderr);
1433 (void)fputs(" [ -O file ]\n", stderr);
1434 (void)fputs("usage: cpio -p [ adlLmuv ] directory\n", stderr);
1435 #else
1436 /* no E, M, R, V, b, k or s */
1437 (void)fputs("usage: cpio -i [-bBcdfkmrsStuvV] [ -C bufsize ]\n", stderr);
1438 (void)fputs(" [ -E file ] [ -H header ] [ -I file [ -M message ] ]\n",
1439 stderr);
1440 (void)fputs(" [ -R id ] [ pattern ... ]\n", stderr);
1441 (void)fputs("usage: cpio -o [-aABcLvV] [ -C bufsize ] [ -H header ]\n",
1442 stderr);
1443 (void)fputs(" [ -O file [ -M message ] ]\n", stderr);
1444 (void)fputs("usage: cpio -p [ adlLmuvV ] [ -R id ] directory\n", stderr);
1445 #endif
1446 exit(1);
1447 }
1448