options.c revision 1.15 1 /* $NetBSD: options.c,v 1.15 1999/01/20 14:45:09 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.15 1999/01/20 14:45:09 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 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 if (opt_add("write_opt=nodir") < 0)
674 tar_usage();
675 break;
676 case 'p':
677 /*
678 * preserve user id, group id, file
679 * mode, access/modification times
680 */
681 pids = 1;
682 pmode = 1;
683 patime = 1;
684 pmtime = 1;
685 break;
686 case 'r':
687 case 'u':
688 /*
689 * append to the archive
690 */
691 act = APPND;
692 break;
693 case 't':
694 /*
695 * list contents of the tape
696 */
697 act = LIST;
698 break;
699 case 'v':
700 /*
701 * verbose operation mode
702 */
703 vflag = 1;
704 break;
705 case 'w':
706 /*
707 * interactive file rename
708 */
709 iflag = 1;
710 break;
711 case 'x':
712 /*
713 * write an archive
714 */
715 act = EXTRACT;
716 break;
717 case 'z':
718 /*
719 * use gzip. Non standard option.
720 */
721 zflag = 1;
722 gzip_program = GZIP_CMD;
723 break;
724 case 'B':
725 /*
726 * Nothing to do here, this is pax default
727 */
728 break;
729 case 'H':
730 /*
731 * follow command line symlinks only
732 */
733 Hflag = 1;
734 break;
735 case 'L':
736 /*
737 * follow symlinks
738 */
739 Lflag = 1;
740 break;
741 case 'P':
742 /*
743 * do not follow symlinks
744 */
745 Lflag = 0;
746 break;
747 case 'X':
748 /*
749 * GNU tar compat: exclude the files listed in optarg
750 */
751 if (tar_gnutar_X_compat(optarg) != 0)
752 tar_usage();
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
1028 /*
1029 * figure out the operation mode of cpio. check that we have not been
1030 * given a bogus set of flags for the operation mode.
1031 */
1032 if (ISLIST(flg)) {
1033 act = LIST;
1034 bflg = flg & BDLIST;
1035 } else if (ISEXTRACT(flg)) {
1036 act = EXTRACT;
1037 bflg = flg & BDEXTR;
1038 } else if (ISARCHIVE(flg)) {
1039 act = ARCHIVE;
1040 bflg = flg & BDARCH;
1041 } else if (ISAPPND(flg)) {
1042 act = APPND;
1043 bflg = flg & BDARCH;
1044 } else if (ISCOPY(flg)) {
1045 act = COPY;
1046 bflg = flg & BDCOPY;
1047 } else
1048 cpio_usage();
1049 if (bflg) {
1050 cpio_usage();
1051 }
1052
1053 /*
1054 * if we are writing (ARCHIVE) we use the default format if the user
1055 * did not specify a format. when we write during an APPEND, we will
1056 * adopt the format of the existing archive if none was supplied.
1057 */
1058 if (!(flg & XF) && (act == ARCHIVE))
1059 frmt = &(fsub[F_BCPIO]);
1060
1061 /*
1062 * process the args as they are interpreted by the operation mode
1063 */
1064 switch (act) {
1065 case LIST:
1066 case EXTRACT:
1067 for (; optind < argc; optind++)
1068 if (pat_add(argv[optind]) < 0)
1069 cpio_usage();
1070 break;
1071 case COPY:
1072 if (optind >= argc) {
1073 tty_warn(0, "Destination directory was not supplied");
1074 cpio_usage();
1075 }
1076 --argc;
1077 dirptr = argv[argc];
1078 /* FALLTHROUGH */
1079 case ARCHIVE:
1080 case APPND:
1081 for (; optind < argc; optind++)
1082 if (ftree_add(argv[optind]) < 0)
1083 cpio_usage();
1084 /*
1085 * no read errors allowed on updates/append operation!
1086 */
1087 maxflt = 0;
1088 break;
1089 }
1090 }
1091
1092 /*
1093 * printflg()
1094 * print out those invalid flag sets found to the user
1095 */
1096
1097 #if __STDC__
1098 static void
1099 printflg(unsigned int flg)
1100 #else
1101 static void
1102 printflg(flg)
1103 unsigned int flg;
1104 #endif
1105 {
1106 int nxt;
1107 int pos = 0;
1108
1109 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1110 while ((nxt = ffs(flg)) != 0) {
1111 flg = flg >> nxt;
1112 pos += nxt;
1113 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1114 }
1115 (void)putc('\n', stderr);
1116 }
1117
1118 /*
1119 * c_frmt()
1120 * comparison routine used by bsearch to find the format specified
1121 * by the user
1122 */
1123
1124 #if __STDC__
1125 static int
1126 c_frmt(const void *a, const void *b)
1127 #else
1128 static int
1129 c_frmt(a, b)
1130 void *a;
1131 void *b;
1132 #endif
1133 {
1134 return(strcmp(((FSUB *)a)->name, ((FSUB *)b)->name));
1135 }
1136
1137 /*
1138 * opt_next()
1139 * called by format specific options routines to get each format specific
1140 * flag and value specified with -o
1141 * Return:
1142 * pointer to next OPLIST entry or NULL (end of list).
1143 */
1144
1145 #if __STDC__
1146 OPLIST *
1147 opt_next(void)
1148 #else
1149 OPLIST *
1150 opt_next()
1151 #endif
1152 {
1153 OPLIST *opt;
1154
1155 if ((opt = ophead) != NULL)
1156 ophead = ophead->fow;
1157 return(opt);
1158 }
1159
1160 /*
1161 * bad_opt()
1162 * generic routine used to complain about a format specific options
1163 * when the format does not support options.
1164 */
1165
1166 #if __STDC__
1167 int
1168 bad_opt(void)
1169 #else
1170 int
1171 bad_opt()
1172 #endif
1173 {
1174 OPLIST *opt;
1175
1176 if (ophead == NULL)
1177 return(0);
1178 /*
1179 * print all we were given
1180 */
1181 tty_warn(1,"These format options are not supported");
1182 while ((opt = opt_next()) != NULL)
1183 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1184 pax_usage();
1185 return(0);
1186 }
1187
1188 /*
1189 * opt_add()
1190 * breaks the value supplied to -o into a option name and value. options
1191 * are given to -o in the form -o name-value,name=value
1192 * mulltiple -o may be specified.
1193 * Return:
1194 * 0 if format in name=value format, -1 if -o is passed junk
1195 */
1196
1197 #if __STDC__
1198 int
1199 opt_add(char *str)
1200 #else
1201 int
1202 opt_add(str)
1203 char *str;
1204 #endif
1205 {
1206 OPLIST *opt;
1207 char *frpt;
1208 char *pt;
1209 char *endpt;
1210
1211 if ((str == NULL) || (*str == '\0')) {
1212 tty_warn(0, "Invalid option name");
1213 return(-1);
1214 }
1215 frpt = endpt = str;
1216
1217 /*
1218 * break into name and values pieces and stuff each one into a
1219 * OPLIST structure. When we know the format, the format specific
1220 * option function will go through this list
1221 */
1222 while ((frpt != NULL) && (*frpt != '\0')) {
1223 if ((endpt = strchr(frpt, ',')) != NULL)
1224 *endpt = '\0';
1225 if ((pt = strchr(frpt, '=')) == NULL) {
1226 tty_warn(0, "Invalid options format");
1227 return(-1);
1228 }
1229 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1230 tty_warn(0, "Unable to allocate space for option list");
1231 return(-1);
1232 }
1233 *pt++ = '\0';
1234 opt->name = frpt;
1235 opt->value = pt;
1236 opt->fow = NULL;
1237 if (endpt != NULL)
1238 frpt = endpt + 1;
1239 else
1240 frpt = NULL;
1241 if (ophead == NULL) {
1242 optail = ophead = opt;
1243 continue;
1244 }
1245 optail->fow = opt;
1246 optail = opt;
1247 }
1248 return(0);
1249 }
1250
1251 /*
1252 * str_offt()
1253 * Convert an expression of the following forms to an off_t > 0.
1254 * 1) A positive decimal number.
1255 * 2) A positive decimal number followed by a b (mult by 512).
1256 * 3) A positive decimal number followed by a k (mult by 1024).
1257 * 4) A positive decimal number followed by a m (mult by 512).
1258 * 5) A positive decimal number followed by a w (mult by sizeof int)
1259 * 6) Two or more positive decimal numbers (with/without k,b or w).
1260 * seperated by x (also * for backwards compatibility), specifying
1261 * the product of the indicated values.
1262 * Return:
1263 * 0 for an error, a positive value o.w.
1264 */
1265
1266 #if __STDC__
1267 static off_t
1268 str_offt(char *val)
1269 #else
1270 static off_t
1271 str_offt(val)
1272 char *val;
1273 #endif
1274 {
1275 char *expr;
1276 off_t num, t;
1277
1278 # ifdef NET2_STAT
1279 num = strtol(val, &expr, 0);
1280 if ((num == LONG_MAX) || (num <= 0) || (expr == val))
1281 # else
1282 num = strtoq(val, &expr, 0);
1283 if ((num == QUAD_MAX) || (num <= 0) || (expr == val))
1284 # endif
1285 return(0);
1286
1287 switch(*expr) {
1288 case 'b':
1289 t = num;
1290 num *= 512;
1291 if (t > num)
1292 return(0);
1293 ++expr;
1294 break;
1295 case 'k':
1296 t = num;
1297 num *= 1024;
1298 if (t > num)
1299 return(0);
1300 ++expr;
1301 break;
1302 case 'm':
1303 t = num;
1304 num *= 1048576;
1305 if (t > num)
1306 return(0);
1307 ++expr;
1308 break;
1309 case 'w':
1310 t = num;
1311 num *= sizeof(int);
1312 if (t > num)
1313 return(0);
1314 ++expr;
1315 break;
1316 }
1317
1318 switch(*expr) {
1319 case '\0':
1320 break;
1321 case '*':
1322 case 'x':
1323 t = num;
1324 num *= str_offt(expr + 1);
1325 if (t > num)
1326 return(0);
1327 break;
1328 default:
1329 return(0);
1330 }
1331 return(num);
1332 }
1333
1334 /*
1335 * no_op()
1336 * for those option functions where the archive format has nothing to do.
1337 * Return:
1338 * 0
1339 */
1340
1341 #if __STDC__
1342 static int
1343 no_op(void)
1344 #else
1345 static int
1346 no_op()
1347 #endif
1348 {
1349 return(0);
1350 }
1351
1352 /*
1353 * pax_usage()
1354 * print the usage summary to the user
1355 */
1356
1357 #if __STDC__
1358 void
1359 pax_usage(void)
1360 #else
1361 void
1362 pax_usage()
1363 #endif
1364 {
1365 (void)fputs("usage: pax [-cdnv] [-E limit] [-f archive] ", stderr);
1366 (void)fputs("[-s replstr] ... [-U user] ...", stderr);
1367 (void)fputs("\n [-G group] ... ", stderr);
1368 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1369 (void)fputs("[pattern ...]\n", stderr);
1370 (void)fputs(" pax -r [-cdiknuvDYZ] [-E limit] ", stderr);
1371 (void)fputs("[-f archive] [-o options] ... \n", stderr);
1372 (void)fputs(" [-p string] ... [-s replstr] ... ", stderr);
1373 (void)fputs("[-U user] ... [-G group] ...\n ", stderr);
1374 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1375 (void)fputs(" [pattern ...]\n", stderr);
1376 (void)fputs(" pax -w [-dituvHLPX] [-b blocksize] ", stderr);
1377 (void)fputs("[ [-a] [-f archive] ] [-x format] \n", stderr);
1378 (void)fputs(" [-B bytes] [-s replstr] ... ", stderr);
1379 (void)fputs("[-o options] ... [-U user] ...", stderr);
1380 (void)fputs("\n [-G group] ... ", stderr);
1381 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1382 (void)fputs("[file ...]\n", stderr);
1383 (void)fputs(" pax -r -w [-diklntuvDHLPXYZ] ", stderr);
1384 (void)fputs("[-p string] ... [-s replstr] ...", stderr);
1385 (void)fputs("\n [-U user] ... [-G group] ... ", stderr);
1386 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1387 (void)fputs("\n [file ...] directory\n", stderr);
1388 exit(1);
1389 /* NOTREACHED */
1390 }
1391
1392 /*
1393 * tar_usage()
1394 * print the usage summary to the user
1395 */
1396
1397 #if __STDC__
1398 void
1399 tar_usage(void)
1400 #else
1401 void
1402 tar_usage()
1403 #endif
1404 {
1405 (void)fputs("usage: tar -{txru}[cevfblmopwBHLPX014578] [tapefile] ",
1406 stderr);
1407 (void)fputs("[blocksize] [exclude-file] file1 file2...\n", stderr);
1408 exit(1);
1409 /* NOTREACHED */
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 /* NOTREACHED */
1448 }
1449