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