options.c revision 1.17 1 /* $NetBSD: options.c,v 1.17 1999/02/02 23:42:41 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.17 1999/02/02 23:42:41 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 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
790 */
791 if (act == ARCHIVE && frmt == NULL)
792 frmt = &(fsub[F_USTAR]);
793
794 /*
795 * process the args as they are interpreted by the operation mode
796 */
797 switch (act) {
798 case LIST:
799 case EXTRACT:
800 default:
801 while (*argv != (char *)NULL)
802 if (pat_add(*argv++) < 0)
803 tar_usage();
804 break;
805 case ARCHIVE:
806 case APPND:
807 while (*argv != (char *)NULL)
808 if (ftree_add(*argv++) < 0)
809 tar_usage();
810 /*
811 * no read errors allowed on updates/append operation!
812 */
813 maxflt = 0;
814 break;
815 }
816 if (!fstdin && ((arcname == (char *)NULL) || (*arcname == '\0'))) {
817 arcname = getenv("TAPE");
818 if ((arcname == (char *)NULL) || (*arcname == '\0'))
819 arcname = DEV_8;
820 }
821 }
822
823 /*
824 * cpio_options()
825 * look at the user specified flags. set globals as required and check if
826 * the user specified a legal set of flags. If not, complain and exit
827 */
828
829 #if __STDC__
830 static void
831 cpio_options(int argc, char **argv)
832 #else
833 static void
834 cpio_options(argc, argv)
835 int argc;
836 char **argv;
837 #endif
838 {
839 FSUB tmp;
840 unsigned int flg = 0;
841 unsigned int bflg = 0;
842 int c, i;
843
844 cpio_mode = uflag = 1;
845 /*
846 * process option flags
847 */
848 while ((c = getoldopt(argc, argv, "ABC:E:H:I:LM:O:R:SVabcdfiklmoprstuv"))
849 != -1) {
850 switch(c) {
851 case 'A':
852 /*
853 * append to an archive
854 */
855 act = APPND;
856 flg |= AF;
857 break;
858 case 'B':
859 /*
860 * set blocksize to 5120
861 */
862 blksz = 5120;
863 break;
864 case 'C':
865 /*
866 * specify blocksize
867 */
868 if ((blksz = (int)str_offt(optarg)) <= 0) {
869 tty_warn(1, "Invalid block size %s", optarg);
870 tar_usage();
871 }
872 break;
873 #ifdef notyet
874 case 'E':
875 arg = optarg;
876 break;
877 #endif
878 case 'H':
879 /*
880 * specify an archive format on write
881 */
882 tmp.name = optarg;
883 frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
884 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
885 if (frmt != NULL) {
886 flg |= XF;
887 break;
888 }
889 tty_warn(1, "Unknown -H format: %s", optarg);
890 (void)fputs("cpio: Known -H formats are:", stderr);
891 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
892 (void)fprintf(stderr, " %s", fsub[i].name);
893 (void)fputs("\n\n", stderr);
894 tar_usage();
895 break;
896 case 'I':
897 case 'O':
898 /*
899 * filename where the archive is stored
900 */
901 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
902 /*
903 * treat a - as stdin
904 */
905 arcname = (char *)0;
906 break;
907 }
908 arcname = optarg;
909 break;
910 case 'L':
911 /*
912 * follow symlinks
913 */
914 Lflag = 1;
915 flg |= CLF;
916 break;
917 #ifdef notyet
918 case 'M':
919 arg = optarg;
920 break;
921 case 'R':
922 arg = optarg;
923 break;
924 #endif
925 case 'S':
926 cpio_swp_head = 1;
927 break;
928 #ifdef notyet
929 case 'V':
930 break;
931 #endif
932 case 'a':
933 /*
934 * preserve access time on filesystem nodes we read
935 */
936 tflag = 1;
937 flg |= TF;
938 break;
939 #ifdef notyet
940 case 'b':
941 break;
942 #endif
943 case 'c':
944 frmt = &fsub[F_SV4CPIO];
945 break;
946 case 'd':
947 /*
948 * pax does this by default ..
949 */
950 flg |= RF;
951 break;
952 case 'f':
953 /*
954 * inverse match on patterns
955 */
956 cflag = 1;
957 flg |= CF;
958 break;
959 case 'i':
960 /*
961 * read the archive
962 */
963 flg |= RF;
964 break;
965 #ifdef notyet
966 case 'k':
967 break;
968 #endif
969 case 'l':
970 /*
971 * try to link src to dest with copy (-rw)
972 */
973 lflag = 1;
974 flg |= LF;
975 break;
976 case 'm':
977 /*
978 * preserve mtime
979 */
980 flg |= PF;
981 pmtime = 1;
982 break;
983 case 'o':
984 /*
985 * write an archive
986 */
987 flg |= WF;
988 break;
989 case 'p':
990 /*
991 * cpio -p is like pax -rw
992 */
993 flg |= RF | WF;
994 break;
995 case 'r':
996 /*
997 * interactive file rename
998 */
999 iflag = 1;
1000 flg |= IF;
1001 break;
1002 #ifdef notyet
1003 case 's':
1004 break;
1005 #endif
1006 case 't':
1007 act = LIST;
1008 break;
1009 case 'u':
1010 /*
1011 * don't ignore those older files
1012 */
1013 uflag = 0;
1014 flg |= UF;
1015 break;
1016 case 'v':
1017 /*
1018 * verbose operation mode
1019 */
1020 vflag = 1;
1021 flg |= VF;
1022 break;
1023 default:
1024 cpio_usage();
1025 break;
1026 }
1027 }
1028
1029 /*
1030 * figure out the operation mode of cpio. check that we have not been
1031 * given a bogus set of flags for the operation mode.
1032 */
1033 if (ISLIST(flg)) {
1034 act = LIST;
1035 bflg = flg & BDLIST;
1036 } else if (ISEXTRACT(flg)) {
1037 act = EXTRACT;
1038 bflg = flg & BDEXTR;
1039 } else if (ISARCHIVE(flg)) {
1040 act = ARCHIVE;
1041 bflg = flg & BDARCH;
1042 } else if (ISAPPND(flg)) {
1043 act = APPND;
1044 bflg = flg & BDARCH;
1045 } else if (ISCOPY(flg)) {
1046 act = COPY;
1047 bflg = flg & BDCOPY;
1048 } else
1049 cpio_usage();
1050 if (bflg) {
1051 cpio_usage();
1052 }
1053
1054 /*
1055 * if we are writing (ARCHIVE) we use the default format if the user
1056 * did not specify a format. when we write during an APPEND, we will
1057 * adopt the format of the existing archive if none was supplied.
1058 */
1059 if (!(flg & XF) && (act == ARCHIVE))
1060 frmt = &(fsub[F_BCPIO]);
1061
1062 /*
1063 * process the args as they are interpreted by the operation mode
1064 */
1065 switch (act) {
1066 case LIST:
1067 case EXTRACT:
1068 for (; optind < argc; optind++)
1069 if (pat_add(argv[optind]) < 0)
1070 cpio_usage();
1071 break;
1072 case COPY:
1073 if (optind >= argc) {
1074 tty_warn(0, "Destination directory was not supplied");
1075 cpio_usage();
1076 }
1077 --argc;
1078 dirptr = argv[argc];
1079 /* FALLTHROUGH */
1080 case ARCHIVE:
1081 case APPND:
1082 for (; optind < argc; optind++)
1083 if (ftree_add(argv[optind]) < 0)
1084 cpio_usage();
1085 /*
1086 * no read errors allowed on updates/append operation!
1087 */
1088 maxflt = 0;
1089 break;
1090 }
1091 }
1092
1093 /*
1094 * printflg()
1095 * print out those invalid flag sets found to the user
1096 */
1097
1098 #if __STDC__
1099 static void
1100 printflg(unsigned int flg)
1101 #else
1102 static void
1103 printflg(flg)
1104 unsigned int flg;
1105 #endif
1106 {
1107 int nxt;
1108 int pos = 0;
1109
1110 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1111 while ((nxt = ffs(flg)) != 0) {
1112 flg = flg >> nxt;
1113 pos += nxt;
1114 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1115 }
1116 (void)putc('\n', stderr);
1117 }
1118
1119 /*
1120 * c_frmt()
1121 * comparison routine used by bsearch to find the format specified
1122 * by the user
1123 */
1124
1125 #if __STDC__
1126 static int
1127 c_frmt(const void *a, const void *b)
1128 #else
1129 static int
1130 c_frmt(a, b)
1131 void *a;
1132 void *b;
1133 #endif
1134 {
1135 return(strcmp(((FSUB *)a)->name, ((FSUB *)b)->name));
1136 }
1137
1138 /*
1139 * opt_next()
1140 * called by format specific options routines to get each format specific
1141 * flag and value specified with -o
1142 * Return:
1143 * pointer to next OPLIST entry or NULL (end of list).
1144 */
1145
1146 #if __STDC__
1147 OPLIST *
1148 opt_next(void)
1149 #else
1150 OPLIST *
1151 opt_next()
1152 #endif
1153 {
1154 OPLIST *opt;
1155
1156 if ((opt = ophead) != NULL)
1157 ophead = ophead->fow;
1158 return(opt);
1159 }
1160
1161 /*
1162 * bad_opt()
1163 * generic routine used to complain about a format specific options
1164 * when the format does not support options.
1165 */
1166
1167 #if __STDC__
1168 int
1169 bad_opt(void)
1170 #else
1171 int
1172 bad_opt()
1173 #endif
1174 {
1175 OPLIST *opt;
1176
1177 if (ophead == NULL)
1178 return(0);
1179 /*
1180 * print all we were given
1181 */
1182 tty_warn(1,"These format options are not supported");
1183 while ((opt = opt_next()) != NULL)
1184 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1185 pax_usage();
1186 return(0);
1187 }
1188
1189 /*
1190 * opt_add()
1191 * breaks the value supplied to -o into a option name and value. options
1192 * are given to -o in the form -o name-value,name=value
1193 * mulltiple -o may be specified.
1194 * Return:
1195 * 0 if format in name=value format, -1 if -o is passed junk
1196 */
1197
1198 #if __STDC__
1199 int
1200 opt_add(char *str)
1201 #else
1202 int
1203 opt_add(str)
1204 char *str;
1205 #endif
1206 {
1207 OPLIST *opt;
1208 char *frpt;
1209 char *pt;
1210 char *endpt;
1211
1212 if ((str == NULL) || (*str == '\0')) {
1213 tty_warn(0, "Invalid option name");
1214 return(-1);
1215 }
1216 frpt = endpt = str;
1217
1218 /*
1219 * break into name and values pieces and stuff each one into a
1220 * OPLIST structure. When we know the format, the format specific
1221 * option function will go through this list
1222 */
1223 while ((frpt != NULL) && (*frpt != '\0')) {
1224 if ((endpt = strchr(frpt, ',')) != NULL)
1225 *endpt = '\0';
1226 if ((pt = strchr(frpt, '=')) == NULL) {
1227 tty_warn(0, "Invalid options format");
1228 return(-1);
1229 }
1230 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1231 tty_warn(0, "Unable to allocate space for option list");
1232 return(-1);
1233 }
1234 *pt++ = '\0';
1235 opt->name = frpt;
1236 opt->value = pt;
1237 opt->fow = NULL;
1238 if (endpt != NULL)
1239 frpt = endpt + 1;
1240 else
1241 frpt = NULL;
1242 if (ophead == NULL) {
1243 optail = ophead = opt;
1244 continue;
1245 }
1246 optail->fow = opt;
1247 optail = opt;
1248 }
1249 return(0);
1250 }
1251
1252 /*
1253 * str_offt()
1254 * Convert an expression of the following forms to an off_t > 0.
1255 * 1) A positive decimal number.
1256 * 2) A positive decimal number followed by a b (mult by 512).
1257 * 3) A positive decimal number followed by a k (mult by 1024).
1258 * 4) A positive decimal number followed by a m (mult by 512).
1259 * 5) A positive decimal number followed by a w (mult by sizeof int)
1260 * 6) Two or more positive decimal numbers (with/without k,b or w).
1261 * seperated by x (also * for backwards compatibility), specifying
1262 * the product of the indicated values.
1263 * Return:
1264 * 0 for an error, a positive value o.w.
1265 */
1266
1267 #if __STDC__
1268 static off_t
1269 str_offt(char *val)
1270 #else
1271 static off_t
1272 str_offt(val)
1273 char *val;
1274 #endif
1275 {
1276 char *expr;
1277 off_t num, t;
1278
1279 # ifdef NET2_STAT
1280 num = strtol(val, &expr, 0);
1281 if ((num == LONG_MAX) || (num <= 0) || (expr == val))
1282 # else
1283 num = strtoq(val, &expr, 0);
1284 if ((num == QUAD_MAX) || (num <= 0) || (expr == val))
1285 # endif
1286 return(0);
1287
1288 switch(*expr) {
1289 case 'b':
1290 t = num;
1291 num *= 512;
1292 if (t > num)
1293 return(0);
1294 ++expr;
1295 break;
1296 case 'k':
1297 t = num;
1298 num *= 1024;
1299 if (t > num)
1300 return(0);
1301 ++expr;
1302 break;
1303 case 'm':
1304 t = num;
1305 num *= 1048576;
1306 if (t > num)
1307 return(0);
1308 ++expr;
1309 break;
1310 case 'w':
1311 t = num;
1312 num *= sizeof(int);
1313 if (t > num)
1314 return(0);
1315 ++expr;
1316 break;
1317 }
1318
1319 switch(*expr) {
1320 case '\0':
1321 break;
1322 case '*':
1323 case 'x':
1324 t = num;
1325 num *= str_offt(expr + 1);
1326 if (t > num)
1327 return(0);
1328 break;
1329 default:
1330 return(0);
1331 }
1332 return(num);
1333 }
1334
1335 /*
1336 * no_op()
1337 * for those option functions where the archive format has nothing to do.
1338 * Return:
1339 * 0
1340 */
1341
1342 #if __STDC__
1343 static int
1344 no_op(void)
1345 #else
1346 static int
1347 no_op()
1348 #endif
1349 {
1350 return(0);
1351 }
1352
1353 /*
1354 * pax_usage()
1355 * print the usage summary to the user
1356 */
1357
1358 #if __STDC__
1359 void
1360 pax_usage(void)
1361 #else
1362 void
1363 pax_usage()
1364 #endif
1365 {
1366 (void)fputs("usage: pax [-cdnv] [-E limit] [-f archive] ", stderr);
1367 (void)fputs("[-s replstr] ... [-U user] ...", stderr);
1368 (void)fputs("\n [-G group] ... ", stderr);
1369 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1370 (void)fputs("[pattern ...]\n", stderr);
1371 (void)fputs(" pax -r [-cdiknuvDYZ] [-E limit] ", stderr);
1372 (void)fputs("[-f archive] [-o options] ... \n", stderr);
1373 (void)fputs(" [-p string] ... [-s replstr] ... ", stderr);
1374 (void)fputs("[-U user] ... [-G group] ...\n ", stderr);
1375 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1376 (void)fputs(" [pattern ...]\n", stderr);
1377 (void)fputs(" pax -w [-dituvHLPX] [-b blocksize] ", stderr);
1378 (void)fputs("[ [-a] [-f archive] ] [-x format] \n", stderr);
1379 (void)fputs(" [-B bytes] [-s replstr] ... ", stderr);
1380 (void)fputs("[-o options] ... [-U user] ...", stderr);
1381 (void)fputs("\n [-G group] ... ", stderr);
1382 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1383 (void)fputs("[file ...]\n", stderr);
1384 (void)fputs(" pax -r -w [-diklntuvDHLPXYZ] ", stderr);
1385 (void)fputs("[-p string] ... [-s replstr] ...", stderr);
1386 (void)fputs("\n [-U user] ... [-G group] ... ", stderr);
1387 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1388 (void)fputs("\n [file ...] directory\n", stderr);
1389 exit(1);
1390 /* NOTREACHED */
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] [exclude-file] file1 file2...\n", stderr);
1409 exit(1);
1410 /* NOTREACHED */
1411 }
1412
1413 /*
1414 * cpio_usage()
1415 * print the usage summary to the user
1416 */
1417
1418 #if __STDC__
1419 void
1420 cpio_usage(void)
1421 #else
1422 void
1423 cpio_usage()
1424 #endif
1425 {
1426
1427 #if 1
1428 (void)fputs(
1429 "usage: cpio -i [-BcdfmrStuv] [ -C blksize ] [ -H header ]\n",
1430 stderr);
1431 (void)fputs(" [ -I file ] [ pattern ... ]\n", stderr);
1432 (void)fputs("usage: cpio -o [-aABcLv] [ -C bufsize ] [ -H header ]\n",
1433 stderr);
1434 (void)fputs(" [ -O file ]\n", stderr);
1435 (void)fputs("usage: cpio -p [ adlLmuv ] directory\n", stderr);
1436 #else
1437 /* no E, M, R, V, b, k or s */
1438 (void)fputs("usage: cpio -i [-bBcdfkmrsStuvV] [ -C bufsize ]\n", stderr);
1439 (void)fputs(" [ -E file ] [ -H header ] [ -I file [ -M message ] ]\n",
1440 stderr);
1441 (void)fputs(" [ -R id ] [ pattern ... ]\n", stderr);
1442 (void)fputs("usage: cpio -o [-aABcLvV] [ -C bufsize ] [ -H header ]\n",
1443 stderr);
1444 (void)fputs(" [ -O file [ -M message ] ]\n", stderr);
1445 (void)fputs("usage: cpio -p [ adlLmuvV ] [ -R id ] directory\n", stderr);
1446 #endif
1447 exit(1);
1448 /* NOTREACHED */
1449 }
1450