file_cmds-116.9.tar.gz
[apple/file_cmds.git] / pax / options.c
1 /* $OpenBSD: options.c,v 1.31 1998/01/22 06:21:29 millert Exp $ */
2 /* $NetBSD: options.c,v 1.6 1996/03/26 23:54:18 mrg Exp $ */
3
4 /*-
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 4/18/94";
44 #else
45 static char rcsid[] __attribute__((__unused__)) = "$OpenBSD: options.c,v 1.31 1998/01/22 06:21:29 millert 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 <string.h>
56 #include <errno.h>
57 #include <unistd.h>
58 #include <stdlib.h>
59 #include <limits.h>
60 #include <paths.h>
61 #include "pax.h"
62 #include "options.h"
63 #include "cpio.h"
64 #include "tar.h"
65 #include "extern.h"
66
67 /*
68 * Routines which handle command line options
69 */
70
71 static char flgch[] = FLGCH; /* list of all possible flags */
72 static OPLIST *ophead = NULL; /* head for format specific options -x */
73 static OPLIST *optail = NULL; /* option tail */
74
75 static int no_op __P((void));
76 static void printflg __P((unsigned int));
77 static int c_frmt __P((const void *, const void *));
78 static off_t str_offt __P((char *));
79 static void pax_options __P((register int, register char **));
80 static void pax_usage __P((void));
81 static void tar_options __P((register int, register char **));
82 static void tar_usage __P((void));
83 static void cpio_options __P((register int, register char **));
84 static void cpio_usage __P((void));
85
86 #define GZIP_CMD "gzip" /* command to run as gzip */
87 #define COMPRESS_CMD "compress" /* command to run as compress */
88
89 /*
90 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
91 * (see pax.h for description of each function)
92 *
93 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
94 * read, end_read, st_write, write, end_write, trail,
95 * rd_data, wr_data, options
96 */
97
98 FSUB fsub[] = {
99 /* 0: OLD BINARY CPIO */
100 {"bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
101 bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, cpio_trail,
102 rd_wrfile, wr_rdfile, bad_opt},
103
104 /* 1: OLD OCTAL CHARACTER CPIO */
105 {"cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
106 cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, cpio_trail,
107 rd_wrfile, wr_rdfile, bad_opt},
108
109 /* 2: SVR4 HEX CPIO */
110 {"sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
111 vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, cpio_trail,
112 rd_wrfile, wr_rdfile, bad_opt},
113
114 /* 3: SVR4 HEX CPIO WITH CRC */
115 {"sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
116 vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, cpio_trail,
117 rd_wrfile, wr_rdfile, bad_opt},
118
119 /* 4: OLD TAR */
120 {"tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
121 tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
122 rd_wrfile, wr_rdfile, tar_opt},
123
124 /* 5: POSIX USTAR */
125 {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
126 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
127 rd_wrfile, wr_rdfile, bad_opt},
128 };
129 #define F_OCPIO 0 /* format when called as cpio -6 */
130 #define F_ACPIO 1 /* format when called as cpio -c */
131 #define F_CPIO 3 /* format when called as cpio */
132 #define F_OTAR 4 /* format when called as tar -o */
133 #define F_TAR 5 /* format when called as tar */
134 #define DEFLT 5 /* default write format from list above */
135
136 /*
137 * ford is the archive search order used by get_arc() to determine what kind
138 * of archive we are dealing with. This helps to properly id archive formats
139 * some formats may be subsets of others....
140 */
141 int ford[] = {5, 4, 3, 2, 1, 0, -1 };
142
143 /*
144 * options()
145 * figure out if we are pax, tar or cpio. Call the appropriate options
146 * parser
147 */
148
149 #ifdef __STDC__
150 void
151 options(register int argc, register char **argv)
152 #else
153 void
154 options(argc, argv)
155 register int argc;
156 register char **argv;
157 #endif
158 {
159
160 /*
161 * Are we acting like pax, tar or cpio (based on argv[0])
162 */
163 if ((argv0 = strrchr(argv[0], '/')) != NULL)
164 argv0++;
165 else
166 argv0 = argv[0];
167
168 if (strcmp(NM_TAR, argv0) == 0)
169 return(tar_options(argc, argv));
170 else if (strcmp(NM_CPIO, argv0) == 0)
171 return(cpio_options(argc, argv));
172 /*
173 * assume pax as the default
174 */
175 argv0 = NM_PAX;
176 return(pax_options(argc, argv));
177 }
178
179 /*
180 * pax_options()
181 * look at the user specified flags. set globals as required and check if
182 * the user specified a legal set of flags. If not, complain and exit
183 */
184
185 #ifdef __STDC__
186 static void
187 pax_options(register int argc, register char **argv)
188 #else
189 static void
190 pax_options(argc, argv)
191 register int argc;
192 register char **argv;
193 #endif
194 {
195 register int c;
196 register int i;
197 unsigned int flg = 0;
198 unsigned int bflg = 0;
199 register char *pt;
200 FSUB tmp;
201 extern char *optarg;
202 extern int optind;
203
204 /*
205 * process option flags
206 */
207 while ((c=getopt(argc,argv,"ab:cdf:iklno:p:rs:tuvwx:zB:DE:G:HLPT:U:XYZ"))
208 != EOF) {
209 switch (c) {
210 case 'a':
211 /*
212 * append
213 */
214 flg |= AF;
215 break;
216 case 'b':
217 /*
218 * specify blocksize
219 */
220 flg |= BF;
221 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
222 paxwarn(1, "Invalid block size %s", optarg);
223 pax_usage();
224 }
225 break;
226 case 'c':
227 /*
228 * inverse match on patterns
229 */
230 cflag = 1;
231 flg |= CF;
232 break;
233 case 'd':
234 /*
235 * match only dir on extract, not the subtree at dir
236 */
237 dflag = 1;
238 flg |= DF;
239 break;
240 case 'f':
241 /*
242 * filename where the archive is stored
243 */
244 arcname = optarg;
245 flg |= FF;
246 break;
247 case 'i':
248 /*
249 * interactive file rename
250 */
251 iflag = 1;
252 flg |= IF;
253 break;
254 case 'k':
255 /*
256 * do not clobber files that exist
257 */
258 kflag = 1;
259 flg |= KF;
260 break;
261 case 'l':
262 /*
263 * try to link src to dest with copy (-rw)
264 */
265 lflag = 1;
266 flg |= LF;
267 break;
268 case 'n':
269 /*
270 * select first match for a pattern only
271 */
272 nflag = 1;
273 flg |= NF;
274 break;
275 case 'o':
276 /*
277 * pass format specific options
278 */
279 flg |= OF;
280 if (opt_add(optarg) < 0)
281 pax_usage();
282 break;
283 case 'p':
284 /*
285 * specify file characteristic options
286 */
287 for (pt = optarg; *pt != '\0'; ++pt) {
288 switch(*pt) {
289 case 'a':
290 /*
291 * do not preserve access time
292 */
293 patime = 0;
294 break;
295 case 'e':
296 /*
297 * preserve user id, group id, file
298 * mode, access/modification times
299 */
300 pids = 1;
301 pmode = 1;
302 patime = 1;
303 pmtime = 1;
304 break;
305 case 'm':
306 /*
307 * do not preserve modification time
308 */
309 pmtime = 0;
310 break;
311 case 'o':
312 /*
313 * preserve uid/gid
314 */
315 pids = 1;
316 break;
317 case 'p':
318 /*
319 * preserver file mode bits
320 */
321 pmode = 1;
322 break;
323 default:
324 paxwarn(1, "Invalid -p string: %c", *pt);
325 pax_usage();
326 break;
327 }
328 }
329 flg |= PF;
330 break;
331 case 'r':
332 /*
333 * read the archive
334 */
335 flg |= RF;
336 break;
337 case 's':
338 /*
339 * file name substitution name pattern
340 */
341 if (rep_add(optarg) < 0) {
342 pax_usage();
343 break;
344 }
345 flg |= SF;
346 break;
347 case 't':
348 /*
349 * preserve access time on filesystem nodes we read
350 */
351 tflag = 1;
352 flg |= TF;
353 break;
354 case 'u':
355 /*
356 * ignore those older files
357 */
358 uflag = 1;
359 flg |= UF;
360 break;
361 case 'v':
362 /*
363 * verbose operation mode
364 */
365 vflag = 1;
366 flg |= VF;
367 break;
368 case 'w':
369 /*
370 * write an archive
371 */
372 flg |= WF;
373 break;
374 case 'x':
375 /*
376 * specify an archive format on write
377 */
378 tmp.name = optarg;
379 if (0 == strcmp("pax", optarg)) { /* alias for ustar */
380 tmp.name = "ustar";
381 }
382 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
383 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL) {
384 flg |= XF;
385 break;
386 }
387 paxwarn(1, "Unknown -x format: %s", optarg);
388 (void)fputs("pax: Known -x formats are:", stderr);
389 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
390 (void)fprintf(stderr, " %s", fsub[i].name);
391 (void)fputs("\n\n", stderr);
392 pax_usage();
393 break;
394 case 'z':
395 /*
396 * use gzip. Non standard option.
397 */
398 zflag = 1;
399 gzip_program = GZIP_CMD;
400 break;
401 case 'B':
402 /*
403 * non-standard option on number of bytes written on a
404 * single archive volume.
405 */
406 if ((wrlimit = str_offt(optarg)) <= 0) {
407 paxwarn(1, "Invalid write limit %s", optarg);
408 pax_usage();
409 }
410 if (wrlimit % BLKMULT) {
411 paxwarn(1, "Write limit is not a %d byte multiple",
412 BLKMULT);
413 pax_usage();
414 }
415 flg |= CBF;
416 break;
417 case 'D':
418 /*
419 * On extraction check file inode change time before the
420 * modification of the file name. Non standard option.
421 */
422 Dflag = 1;
423 flg |= CDF;
424 break;
425 case 'E':
426 /*
427 * non-standard limit on read faults
428 * 0 indicates stop after first error, values
429 * indicate a limit, "NONE" try forever
430 */
431 flg |= CEF;
432 if (strcmp(NONE, optarg) == 0)
433 maxflt = -1;
434 else if ((maxflt = atoi(optarg)) < 0) {
435 paxwarn(1, "Error count value must be positive");
436 pax_usage();
437 }
438 break;
439 case 'G':
440 /*
441 * non-standard option for selecting files within an
442 * archive by group (gid or name)
443 */
444 if (grp_add(optarg) < 0) {
445 pax_usage();
446 break;
447 }
448 flg |= CGF;
449 break;
450 case 'H':
451 /*
452 * follow command line symlinks only
453 */
454 Hflag = 1;
455 flg |= CHF;
456 break;
457 case 'L':
458 /*
459 * follow symlinks
460 */
461 Lflag = 1;
462 flg |= CLF;
463 break;
464 case 'P':
465 /*
466 * do NOT follow symlinks (default)
467 */
468 Lflag = 0;
469 flg |= CPF;
470 break;
471 case 'T':
472 /*
473 * non-standard option for selecting files within an
474 * archive by modification time range (lower,upper)
475 */
476 if (trng_add(optarg) < 0) {
477 pax_usage();
478 break;
479 }
480 flg |= CTF;
481 break;
482 case 'U':
483 /*
484 * non-standard option for selecting files within an
485 * archive by user (uid or name)
486 */
487 if (usr_add(optarg) < 0) {
488 pax_usage();
489 break;
490 }
491 flg |= CUF;
492 break;
493 case 'X':
494 /*
495 * do not pass over mount points in the file system
496 */
497 Xflag = 1;
498 flg |= CXF;
499 break;
500 case 'Y':
501 /*
502 * On extraction check file inode change time after the
503 * modification of the file name. Non standard option.
504 */
505 Yflag = 1;
506 flg |= CYF;
507 break;
508 case 'Z':
509 /*
510 * On extraction check modification time after the
511 * modification of the file name. Non standard option.
512 */
513 Zflag = 1;
514 flg |= CZF;
515 break;
516 default:
517 pax_usage();
518 break;
519 }
520 }
521
522 /*
523 * figure out the operation mode of pax read,write,extract,copy,append
524 * or list. check that we have not been given a bogus set of flags
525 * for the operation mode.
526 */
527 if (ISLIST(flg)) {
528 act = LIST;
529 bflg = flg & BDLIST;
530 } else if (ISEXTRACT(flg)) {
531 act = EXTRACT;
532 bflg = flg & BDEXTR;
533 } else if (ISARCHIVE(flg)) {
534 act = ARCHIVE;
535 bflg = flg & BDARCH;
536 } else if (ISAPPND(flg)) {
537 act = APPND;
538 bflg = flg & BDARCH;
539 } else if (ISCOPY(flg)) {
540 act = COPY;
541 bflg = flg & BDCOPY;
542 } else
543 pax_usage();
544 if (bflg) {
545 printflg(flg);
546 pax_usage();
547 }
548
549 /*
550 * if we are writing (ARCHIVE) we use the default format if the user
551 * did not specify a format. when we write during an APPEND, we will
552 * adopt the format of the existing archive if none was supplied.
553 */
554 if (!(flg & XF) && (act == ARCHIVE))
555 frmt = &(fsub[DEFLT]);
556
557 /*
558 * process the args as they are interpreted by the operation mode
559 */
560 switch (act) {
561 case LIST:
562 case EXTRACT:
563 for (; optind < argc; optind++)
564 if (pat_add(argv[optind], NULL) < 0)
565 pax_usage();
566 break;
567 case COPY:
568 if (optind >= argc) {
569 paxwarn(0, "Destination directory was not supplied");
570 pax_usage();
571 }
572 --argc;
573 dirptr = argv[argc];
574 /* FALL THROUGH */
575 case ARCHIVE:
576 case APPND:
577 for (; optind < argc; optind++)
578 if (ftree_add(argv[optind], 0) < 0)
579 pax_usage();
580 /*
581 * no read errors allowed on updates/append operation!
582 */
583 maxflt = 0;
584 break;
585 }
586 }
587
588
589 /*
590 * tar_options()
591 * look at the user specified flags. set globals as required and check if
592 * the user specified a legal set of flags. If not, complain and exit
593 */
594
595 #ifdef __STDC__
596 static void
597 tar_options(register int argc, register char **argv)
598 #else
599 static void
600 tar_options(argc, argv)
601 register int argc;
602 register char **argv;
603 #endif
604 {
605 register int c;
606 int fstdin = 0;
607 int Oflag = 0;
608
609 /*
610 * Set default values.
611 */
612 rmleadslash = 1;
613
614 /*
615 * process option flags
616 */
617 while ((c = getoldopt(argc, argv,
618 "b:cef:hmopruts:vwxzBC:HLOPXZ014578"))
619 != EOF) {
620 switch(c) {
621 case 'b':
622 /*
623 * specify blocksize in 512-byte blocks
624 */
625 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
626 paxwarn(1, "Invalid block size %s", optarg);
627 tar_usage();
628 }
629 wrblksz *= 512; /* XXX - check for int oflow */
630 break;
631 case 'c':
632 /*
633 * create an archive
634 */
635 act = ARCHIVE;
636 break;
637 case 'e':
638 /*
639 * stop after first error
640 */
641 maxflt = 0;
642 break;
643 case 'f':
644 /*
645 * filename where the archive is stored
646 */
647 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
648 /*
649 * treat a - as stdin
650 */
651 fstdin = 1;
652 arcname = NULL;
653 break;
654 }
655 fstdin = 0;
656 arcname = optarg;
657 break;
658 case 'h':
659 /*
660 * follow symlinks
661 */
662 Lflag = 1;
663 break;
664 case 'm':
665 /*
666 * do not preserve modification time
667 */
668 pmtime = 0;
669 break;
670 case 'o':
671 if (opt_add("write_opt=nodir") < 0)
672 tar_usage();
673 case 'O':
674 Oflag = 1;
675 break;
676 case 'p':
677 /*
678 * preserve user id, group id, file
679 * mode, access/modification times
680 */
681 pids = 1;
682 pmode = 1;
683 patime = 1;
684 pmtime = 1;
685 break;
686 case 'r':
687 case 'u':
688 /*
689 * append to the archive
690 */
691 act = APPND;
692 break;
693 case 's':
694 /*
695 * file name substitution name pattern
696 */
697 if (rep_add(optarg) < 0) {
698 tar_usage();
699 break;
700 }
701 break;
702 case 't':
703 /*
704 * list contents of the tape
705 */
706 act = LIST;
707 break;
708 case 'v':
709 /*
710 * verbose operation mode
711 */
712 vflag++;
713 break;
714 case 'w':
715 /*
716 * interactive file rename
717 */
718 iflag = 1;
719 break;
720 case 'x':
721 /*
722 * write an archive, preserve ids if root
723 */
724 act = EXTRACT;
725 if (geteuid() == 0)
726 pids = 1;
727 break;
728 case 'z':
729 /*
730 * use gzip. Non standard option.
731 */
732 zflag = 1;
733 gzip_program = GZIP_CMD;
734 break;
735 case 'B':
736 /*
737 * Nothing to do here, this is pax default
738 */
739 break;
740 case 'C':
741 chdname = optarg;
742 break;
743 case 'H':
744 /*
745 * follow command line symlinks only
746 */
747 Hflag = 1;
748 break;
749 case 'L':
750 /*
751 * follow symlinks
752 */
753 Lflag = 1;
754 break;
755 case 'P':
756 /*
757 * do not remove leading '/' from pathnames
758 */
759 rmleadslash = 0;
760 break;
761 case 'X':
762 /*
763 * do not pass over mount points in the file system
764 */
765 Xflag = 1;
766 break;
767 case 'Z':
768 /*
769 * use compress.
770 */
771 zflag = 1;
772 gzip_program = COMPRESS_CMD;
773 break;
774 case '0':
775 arcname = DEV_0;
776 break;
777 case '1':
778 arcname = DEV_1;
779 break;
780 case '4':
781 arcname = DEV_4;
782 break;
783 case '5':
784 arcname = DEV_5;
785 break;
786 case '7':
787 arcname = DEV_7;
788 break;
789 case '8':
790 arcname = DEV_8;
791 break;
792 default:
793 tar_usage();
794 break;
795 }
796 }
797 argc -= optind;
798 argv += optind;
799
800 /* Traditional tar behaviour (pax wants to read filelist from stdin) */
801 if ((act == ARCHIVE || act == APPND) && argc == 0)
802 exit(0);
803
804 /*
805 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
806 * (unless -o specified)
807 */
808 if (act == ARCHIVE || act == APPND)
809 frmt = &(fsub[Oflag ? F_OTAR : F_TAR]);
810 else if (Oflag) {
811 paxwarn(1, "The -O/-o options are only valid when writing an archive");
812 tar_usage(); /* only valid when writing */
813 }
814
815 /*
816 * process the args as they are interpreted by the operation mode
817 */
818 switch (act) {
819 case LIST:
820 case EXTRACT:
821 default:
822 {
823 int sawpat = 0;
824
825 while (*argv != NULL) {
826 if (strcmp(*argv, "-C") == 0) {
827 if(*++argv == NULL)
828 break;
829 chdname = *argv++;
830
831 continue;
832 }
833 if (pat_add(*argv++, chdname) < 0)
834 tar_usage();
835 sawpat++;
836 }
837 /*
838 * if patterns were added, we are doing chdir()
839 * on a file-by-file basis, else, just one
840 * global chdir (if any) after opening input.
841 */
842 if (sawpat > 0)
843 chdname = NULL;
844 }
845 break;
846 case ARCHIVE:
847 case APPND:
848 if (chdname != NULL) { /* initial chdir() */
849 if (ftree_add(chdname, 1) < 0)
850 tar_usage();
851 }
852
853 while (*argv != NULL) {
854 if (!strcmp(*argv, "-C")) {
855 if (*++argv == NULL)
856 break;
857 if (ftree_add(*argv++, 1) < 0)
858 tar_usage();
859 } else {
860 if (ftree_add(*argv++, 0) < 0)
861 tar_usage();
862 }
863 }
864 /*
865 * no read errors allowed on updates/append operation!
866 */
867 maxflt = 0;
868 break;
869 }
870 if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
871 arcname = getenv("TAPE");
872 if ((arcname == NULL) || (*arcname == '\0'))
873 arcname = _PATH_DEFTAPE;
874 }
875 }
876
877 int
878 mkpath(path)
879 char *path;
880 {
881 struct stat sb;
882 register char *slash;
883 int done = 0;
884
885 slash = path;
886
887 while (!done) {
888 slash += strspn(slash, "/");
889 slash += strcspn(slash, "/");
890
891 done = (*slash == '\0');
892 *slash = '\0';
893
894 if (stat(path, &sb)) {
895 if (errno != ENOENT || mkdir(path, 0777)) {
896 paxwarn(1, "%s", path);
897 return (-1);
898 }
899 } else if (!S_ISDIR(sb.st_mode)) {
900 syswarn(1, ENOTDIR, "%s", path);
901 return (-1);
902 }
903
904 if (!done)
905 *slash = '/';
906 }
907
908 return (0);
909 }
910 /*
911 * cpio_options()
912 * look at the user specified flags. set globals as required and check if
913 * the user specified a legal set of flags. If not, complain and exit
914 */
915
916 #ifdef __STDC__
917 static void
918 cpio_options(register int argc, register char **argv)
919 #else
920 static void
921 cpio_options(argc, argv)
922 register int argc;
923 register char **argv;
924 #endif
925 {
926 register int c, i;
927 size_t len;
928 char *str;
929 FSUB tmp;
930 FILE *fp;
931
932 kflag = 1;
933 pids = 1;
934 pmode = 1;
935 pmtime = 0;
936 arcname = NULL;
937 dflag = 1;
938 act = -1;
939 nodirs = 1;
940 while ((c=getopt(argc,argv,"abcdfiklmoprstuvzABC:E:F:H:I:LO:SZ6")) != EOF)
941 switch (c) {
942 case 'a':
943 /*
944 * preserve access time on files read
945 */
946 tflag = 1;
947 break;
948 case 'b':
949 /*
950 * swap bytes and half-words when reading data
951 */
952 break;
953 case 'c':
954 /*
955 * ASCII cpio header
956 */
957 frmt = &(fsub[F_ACPIO]);
958 break;
959 case 'd':
960 /*
961 * create directories as needed
962 */
963 nodirs = 0;
964 break;
965 case 'f':
966 /*
967 * invert meaning of pattern list
968 */
969 cflag = 1;
970 break;
971 case 'i':
972 /*
973 * restore an archive
974 */
975 act = EXTRACT;
976 break;
977 case 'k':
978 break;
979 case 'l':
980 /*
981 * use links instead of copies when possible
982 */
983 lflag = 1;
984 break;
985 case 'm':
986 /*
987 * preserve modification time
988 */
989 pmtime = 1;
990 break;
991 case 'o':
992 /*
993 * create an archive
994 */
995 act = ARCHIVE;
996 frmt = &(fsub[F_CPIO]);
997 break;
998 case 'p':
999 /*
1000 * copy-pass mode
1001 */
1002 act = COPY;
1003 break;
1004 case 'r':
1005 /*
1006 * interactively rename files
1007 */
1008 iflag = 1;
1009 break;
1010 case 's':
1011 /*
1012 * swap bytes after reading data
1013 */
1014 break;
1015 case 't':
1016 /*
1017 * list contents of archive
1018 */
1019 act = LIST;
1020 break;
1021 case 'u':
1022 /*
1023 * replace newer files
1024 */
1025 kflag = 0;
1026 break;
1027 case 'v':
1028 /*
1029 * verbose operation mode
1030 */
1031 vflag = 1;
1032 break;
1033 case 'z':
1034 /*
1035 * use gzip. Non standard option.
1036 */
1037 zflag = 1;
1038 gzip_program = GZIP_CMD;
1039 break;
1040 case 'A':
1041 /*
1042 * append mode
1043 */
1044 act = APPND;
1045 break;
1046 case 'B':
1047 /*
1048 * Use 5120 byte block size
1049 */
1050 wrblksz = 5120;
1051 break;
1052 case 'C':
1053 /*
1054 * set block size in bytes
1055 */
1056 wrblksz = atoi(optarg);
1057 break;
1058 case 'E':
1059 /*
1060 * file with patterns to extract or list
1061 */
1062 if ((fp = fopen(optarg, "r")) == NULL) {
1063 paxwarn(1, "Unable to open file '%s' for read", optarg);
1064 cpio_usage();
1065 }
1066 while ((str = fgetln(fp, &len)) != NULL) {
1067 str[len - 1] = '\0';
1068 pat_add(str, NULL);
1069 }
1070 fclose(fp);
1071 break;
1072 case 'F':
1073 case 'I':
1074 case 'O':
1075 /*
1076 * filename where the archive is stored
1077 */
1078 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1079 /*
1080 * treat a - as stdin
1081 */
1082 arcname = NULL;
1083 break;
1084 }
1085 arcname = optarg;
1086 break;
1087 case 'H':
1088 /*
1089 * specify an archive format on write
1090 */
1091 tmp.name = optarg;
1092 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1093 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL)
1094 break;
1095 paxwarn(1, "Unknown -H format: %s", optarg);
1096 (void)fputs("cpio: Known -H formats are:", stderr);
1097 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1098 (void)fprintf(stderr, " %s", fsub[i].name);
1099 (void)fputs("\n\n", stderr);
1100 cpio_usage();
1101 break;
1102 case 'L':
1103 /*
1104 * follow symbolic links
1105 */
1106 Lflag = 1;
1107 break;
1108 case 'S':
1109 /*
1110 * swap halfwords after reading data
1111 */
1112 break;
1113 case 'Z':
1114 /*
1115 * use compress. Non standard option.
1116 */
1117 zflag = 1;
1118 gzip_program = COMPRESS_CMD;
1119 break;
1120 case '6':
1121 /*
1122 * process Version 6 cpio format
1123 */
1124 frmt = &(fsub[F_OCPIO]);
1125 break;
1126 case '?':
1127 default:
1128 cpio_usage();
1129 break;
1130 }
1131 argc -= optind;
1132 argv += optind;
1133
1134 /*
1135 * process the args as they are interpreted by the operation mode
1136 */
1137 switch (act) {
1138 case LIST:
1139 case EXTRACT:
1140 while (*argv != NULL)
1141 if (pat_add(*argv++, NULL) < 0)
1142 cpio_usage();
1143 break;
1144 case COPY:
1145 if (*argv == NULL) {
1146 paxwarn(0, "Destination directory was not supplied");
1147 cpio_usage();
1148 }
1149 dirptr = *argv;
1150 if (mkpath(dirptr) < 0)
1151 cpio_usage();
1152 --argc;
1153 ++argv;
1154 /* FALL THROUGH */
1155 case ARCHIVE:
1156 case APPND:
1157 if (*argv != NULL)
1158 cpio_usage();
1159 /*
1160 * no read errors allowed on updates/append operation!
1161 */
1162 maxflt = 0;
1163 while ((str = fgetln(stdin, &len)) != NULL) {
1164 str[len - 1] = '\0';
1165 ftree_add(strdup(str), NULL);
1166 }
1167 break;
1168 default:
1169 cpio_usage();
1170 break;
1171 }
1172 }
1173
1174 /*
1175 * printflg()
1176 * print out those invalid flag sets found to the user
1177 */
1178
1179 #ifdef __STDC__
1180 static void
1181 printflg(unsigned int flg)
1182 #else
1183 static void
1184 printflg(flg)
1185 unsigned int flg;
1186 #endif
1187 {
1188 int nxt;
1189 int pos = 0;
1190
1191 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1192 while ((nxt = ffs(flg)) != 0) {
1193 flg = flg >> nxt;
1194 pos += nxt;
1195 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1196 }
1197 (void)putc('\n', stderr);
1198 }
1199
1200 /*
1201 * c_frmt()
1202 * comparison routine used by bsearch to find the format specified
1203 * by the user
1204 */
1205
1206 #ifdef __STDC__
1207 static int
1208 c_frmt(const void *a, const void *b)
1209 #else
1210 static int
1211 c_frmt(a, b)
1212 void *a;
1213 void *b;
1214 #endif
1215 {
1216 return(strcmp(((FSUB *)a)->name, ((FSUB *)b)->name));
1217 }
1218
1219 /*
1220 * opt_next()
1221 * called by format specific options routines to get each format specific
1222 * flag and value specified with -o
1223 * Return:
1224 * pointer to next OPLIST entry or NULL (end of list).
1225 */
1226
1227 #ifdef __STDC__
1228 OPLIST *
1229 opt_next(void)
1230 #else
1231 OPLIST *
1232 opt_next()
1233 #endif
1234 {
1235 OPLIST *opt;
1236
1237 if ((opt = ophead) != NULL)
1238 ophead = ophead->fow;
1239 return(opt);
1240 }
1241
1242 /*
1243 * bad_opt()
1244 * generic routine used to complain about a format specific options
1245 * when the format does not support options.
1246 */
1247
1248 #ifdef __STDC__
1249 int
1250 bad_opt(void)
1251 #else
1252 int
1253 bad_opt()
1254 #endif
1255 {
1256 register OPLIST *opt;
1257
1258 if (ophead == NULL)
1259 return(0);
1260 /*
1261 * print all we were given
1262 */
1263 paxwarn(1,"These format options are not supported");
1264 while ((opt = opt_next()) != NULL)
1265 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1266 pax_usage();
1267 return(0);
1268 }
1269
1270 /*
1271 * opt_add()
1272 * breaks the value supplied to -o into a option name and value. options
1273 * are given to -o in the form -o name-value,name=value
1274 * mulltiple -o may be specified.
1275 * Return:
1276 * 0 if format in name=value format, -1 if -o is passed junk
1277 */
1278
1279 #ifdef __STDC__
1280 int
1281 opt_add(register char *str)
1282 #else
1283 int
1284 opt_add(str)
1285 register char *str;
1286 #endif
1287 {
1288 register OPLIST *opt;
1289 register char *frpt;
1290 register char *pt;
1291 register char *endpt;
1292
1293 if ((str == NULL) || (*str == '\0')) {
1294 paxwarn(0, "Invalid option name");
1295 return(-1);
1296 }
1297 if ((str = strdup(str)) == NULL) {
1298 paxwarn(0, "Unable to allocate space for option list");
1299 return(-1);
1300 }
1301 frpt = endpt = str;
1302
1303 /*
1304 * break into name and values pieces and stuff each one into a
1305 * OPLIST structure. When we know the format, the format specific
1306 * option function will go through this list
1307 */
1308 while ((frpt != NULL) && (*frpt != '\0')) {
1309 if ((endpt = strchr(frpt, ',')) != NULL)
1310 *endpt = '\0';
1311 if ((pt = strchr(frpt, '=')) == NULL) {
1312 paxwarn(0, "Invalid options format");
1313 free(str);
1314 return(-1);
1315 }
1316 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1317 paxwarn(0, "Unable to allocate space for option list");
1318 free(str);
1319 return(-1);
1320 }
1321 *pt++ = '\0';
1322 opt->name = frpt;
1323 opt->value = pt;
1324 opt->fow = NULL;
1325 if (endpt != NULL)
1326 frpt = endpt + 1;
1327 else
1328 frpt = NULL;
1329 if (ophead == NULL) {
1330 optail = ophead = opt;
1331 continue;
1332 }
1333 optail->fow = opt;
1334 optail = opt;
1335 }
1336 return(0);
1337 }
1338
1339 /*
1340 * str_offt()
1341 * Convert an expression of the following forms to an off_t > 0.
1342 * 1) A positive decimal number.
1343 * 2) A positive decimal number followed by a b (mult by 512).
1344 * 3) A positive decimal number followed by a k (mult by 1024).
1345 * 4) A positive decimal number followed by a m (mult by 512).
1346 * 5) A positive decimal number followed by a w (mult by sizeof int)
1347 * 6) Two or more positive decimal numbers (with/without k,b or w).
1348 * seperated by x (also * for backwards compatibility), specifying
1349 * the product of the indicated values.
1350 * Return:
1351 * 0 for an error, a positive value o.w.
1352 */
1353
1354 #ifdef __STDC__
1355 static off_t
1356 str_offt(char *val)
1357 #else
1358 static off_t
1359 str_offt(val)
1360 char *val;
1361 #endif
1362 {
1363 char *expr;
1364 off_t num, t;
1365
1366 # ifdef NET2_STAT
1367 num = strtol(val, &expr, 0);
1368 if ((num == LONG_MAX) || (num <= 0) || (expr == val))
1369 # else
1370 num = strtoq(val, &expr, 0);
1371 if ((num == QUAD_MAX) || (num <= 0) || (expr == val))
1372 # endif
1373 return(0);
1374
1375 switch(*expr) {
1376 case 'b':
1377 t = num;
1378 num *= 512;
1379 if (t > num)
1380 return(0);
1381 ++expr;
1382 break;
1383 case 'k':
1384 t = num;
1385 num *= 1024;
1386 if (t > num)
1387 return(0);
1388 ++expr;
1389 break;
1390 case 'm':
1391 t = num;
1392 num *= 1048576;
1393 if (t > num)
1394 return(0);
1395 ++expr;
1396 break;
1397 case 'w':
1398 t = num;
1399 num *= sizeof(int);
1400 if (t > num)
1401 return(0);
1402 ++expr;
1403 break;
1404 }
1405
1406 switch(*expr) {
1407 case '\0':
1408 break;
1409 case '*':
1410 case 'x':
1411 t = num;
1412 num *= str_offt(expr + 1);
1413 if (t > num)
1414 return(0);
1415 break;
1416 default:
1417 return(0);
1418 }
1419 return(num);
1420 }
1421
1422 /*
1423 * no_op()
1424 * for those option functions where the archive format has nothing to do.
1425 * Return:
1426 * 0
1427 */
1428
1429 #ifdef __STDC__
1430 static int
1431 no_op(void)
1432 #else
1433 static int
1434 no_op()
1435 #endif
1436 {
1437 return(0);
1438 }
1439
1440 /*
1441 * pax_usage()
1442 * print the usage summary to the user
1443 */
1444
1445 #ifdef __STDC__
1446 void
1447 pax_usage(void)
1448 #else
1449 void
1450 pax_usage()
1451 #endif
1452 {
1453 (void)fputs("usage: pax [-cdnv] [-E limit] [-f archive] ", stderr);
1454 (void)fputs("[-s replstr] ... [-U user] ...", stderr);
1455 (void)fputs("\n [-G group] ... ", stderr);
1456 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1457 (void)fputs("[pattern ...]\n", stderr);
1458 (void)fputs(" pax -r [-cdiknuvDYZ] [-E limit] ", stderr);
1459 (void)fputs("[-f archive] [-o options] ... \n", stderr);
1460 (void)fputs(" [-p string] ... [-s replstr] ... ", stderr);
1461 (void)fputs("[-U user] ... [-G group] ...\n ", stderr);
1462 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1463 (void)fputs(" [pattern ...]\n", stderr);
1464 (void)fputs(" pax -w [-dituvHLPX] [-b blocksize] ", stderr);
1465 (void)fputs("[ [-a] [-f archive] ] [-x format] \n", stderr);
1466 (void)fputs(" [-B bytes] [-s replstr] ... ", stderr);
1467 (void)fputs("[-o options] ... [-U user] ...", stderr);
1468 (void)fputs("\n [-G group] ... ", stderr);
1469 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1470 (void)fputs("[file ...]\n", stderr);
1471 (void)fputs(" pax -r -w [-diklntuvDHLPXYZ] ", stderr);
1472 (void)fputs("[-p string] ... [-s replstr] ...", stderr);
1473 (void)fputs("\n [-U user] ... [-G group] ... ", stderr);
1474 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1475 (void)fputs("\n [file ...] directory\n", stderr);
1476 exit(1);
1477 }
1478
1479 /*
1480 * tar_usage()
1481 * print the usage summary to the user
1482 */
1483
1484 #ifdef __STDC__
1485 void
1486 tar_usage(void)
1487 #else
1488 void
1489 tar_usage()
1490 #endif
1491 {
1492 (void)fputs("usage: tar -{txru}[cevfbmopswzBHLPXZ014578] [tapefile] ",
1493 stderr);
1494 (void)fputs("[blocksize] [replstr] [-C directory] file1 file2...\n",
1495 stderr);
1496 exit(1);
1497 }
1498
1499 /*
1500 * cpio_usage()
1501 * print the usage summary to the user
1502 */
1503
1504 #ifdef __STDC__
1505 void
1506 cpio_usage(void)
1507 #else
1508 void
1509 cpio_usage()
1510 #endif
1511 {
1512 (void)fputs("usage: cpio -o [-aABcLvVzZ] [-C bytes] [-H format] [-O archive]\n", stderr);
1513 (void)fputs(" [-F archive] < name-list [> archive]\n", stderr);
1514 (void)fputs(" cpio -i [-bBcdfmnrsStuvVzZ6] [-C bytes] [-E file] [-H format]\n", stderr);
1515 (void)fputs(" [-I archive] [-F archive] [pattern...] [< archive]\n", stderr);
1516 (void)fputs(" cpio -p [-adlLmuvV] destination-directory < name-list\n", stderr);
1517 exit(1);
1518 }