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