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