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