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