file_cmds-45.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 ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
380 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL) {
381 flg |= XF;
382 break;
383 }
384 paxwarn(1, "Unknown -x format: %s", optarg);
385 (void)fputs("pax: Known -x formats are:", stderr);
386 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
387 (void)fprintf(stderr, " %s", fsub[i].name);
388 (void)fputs("\n\n", stderr);
389 pax_usage();
390 break;
391 case 'z':
392 /*
393 * use gzip. Non standard option.
394 */
395 zflag = 1;
396 gzip_program = GZIP_CMD;
397 break;
398 case 'B':
399 /*
400 * non-standard option on number of bytes written on a
401 * single archive volume.
402 */
403 if ((wrlimit = str_offt(optarg)) <= 0) {
404 paxwarn(1, "Invalid write limit %s", optarg);
405 pax_usage();
406 }
407 if (wrlimit % BLKMULT) {
408 paxwarn(1, "Write limit is not a %d byte multiple",
409 BLKMULT);
410 pax_usage();
411 }
412 flg |= CBF;
413 break;
414 case 'D':
415 /*
416 * On extraction check file inode change time before the
417 * modification of the file name. Non standard option.
418 */
419 Dflag = 1;
420 flg |= CDF;
421 break;
422 case 'E':
423 /*
424 * non-standard limit on read faults
425 * 0 indicates stop after first error, values
426 * indicate a limit, "NONE" try forever
427 */
428 flg |= CEF;
429 if (strcmp(NONE, optarg) == 0)
430 maxflt = -1;
431 else if ((maxflt = atoi(optarg)) < 0) {
432 paxwarn(1, "Error count value must be positive");
433 pax_usage();
434 }
435 break;
436 case 'G':
437 /*
438 * non-standard option for selecting files within an
439 * archive by group (gid or name)
440 */
441 if (grp_add(optarg) < 0) {
442 pax_usage();
443 break;
444 }
445 flg |= CGF;
446 break;
447 case 'H':
448 /*
449 * follow command line symlinks only
450 */
451 Hflag = 1;
452 flg |= CHF;
453 break;
454 case 'L':
455 /*
456 * follow symlinks
457 */
458 Lflag = 1;
459 flg |= CLF;
460 break;
461 case 'P':
462 /*
463 * do NOT follow symlinks (default)
464 */
465 Lflag = 0;
466 flg |= CPF;
467 break;
468 case 'T':
469 /*
470 * non-standard option for selecting files within an
471 * archive by modification time range (lower,upper)
472 */
473 if (trng_add(optarg) < 0) {
474 pax_usage();
475 break;
476 }
477 flg |= CTF;
478 break;
479 case 'U':
480 /*
481 * non-standard option for selecting files within an
482 * archive by user (uid or name)
483 */
484 if (usr_add(optarg) < 0) {
485 pax_usage();
486 break;
487 }
488 flg |= CUF;
489 break;
490 case 'X':
491 /*
492 * do not pass over mount points in the file system
493 */
494 Xflag = 1;
495 flg |= CXF;
496 break;
497 case 'Y':
498 /*
499 * On extraction check file inode change time after the
500 * modification of the file name. Non standard option.
501 */
502 Yflag = 1;
503 flg |= CYF;
504 break;
505 case 'Z':
506 /*
507 * On extraction check modification time after the
508 * modification of the file name. Non standard option.
509 */
510 Zflag = 1;
511 flg |= CZF;
512 break;
513 default:
514 pax_usage();
515 break;
516 }
517 }
518
519 /*
520 * figure out the operation mode of pax read,write,extract,copy,append
521 * or list. check that we have not been given a bogus set of flags
522 * for the operation mode.
523 */
524 if (ISLIST(flg)) {
525 act = LIST;
526 bflg = flg & BDLIST;
527 } else if (ISEXTRACT(flg)) {
528 act = EXTRACT;
529 bflg = flg & BDEXTR;
530 } else if (ISARCHIVE(flg)) {
531 act = ARCHIVE;
532 bflg = flg & BDARCH;
533 } else if (ISAPPND(flg)) {
534 act = APPND;
535 bflg = flg & BDARCH;
536 } else if (ISCOPY(flg)) {
537 act = COPY;
538 bflg = flg & BDCOPY;
539 } else
540 pax_usage();
541 if (bflg) {
542 printflg(flg);
543 pax_usage();
544 }
545
546 /*
547 * if we are writing (ARCHIVE) we use the default format if the user
548 * did not specify a format. when we write during an APPEND, we will
549 * adopt the format of the existing archive if none was supplied.
550 */
551 if (!(flg & XF) && (act == ARCHIVE))
552 frmt = &(fsub[DEFLT]);
553
554 /*
555 * process the args as they are interpreted by the operation mode
556 */
557 switch (act) {
558 case LIST:
559 case EXTRACT:
560 for (; optind < argc; optind++)
561 if (pat_add(argv[optind], NULL) < 0)
562 pax_usage();
563 break;
564 case COPY:
565 if (optind >= argc) {
566 paxwarn(0, "Destination directory was not supplied");
567 pax_usage();
568 }
569 --argc;
570 dirptr = argv[argc];
571 /* FALL THROUGH */
572 case ARCHIVE:
573 case APPND:
574 for (; optind < argc; optind++)
575 if (ftree_add(argv[optind], 0) < 0)
576 pax_usage();
577 /*
578 * no read errors allowed on updates/append operation!
579 */
580 maxflt = 0;
581 break;
582 }
583 }
584
585
586 /*
587 * tar_options()
588 * look at the user specified flags. set globals as required and check if
589 * the user specified a legal set of flags. If not, complain and exit
590 */
591
592 #ifdef __STDC__
593 static void
594 tar_options(register int argc, register char **argv)
595 #else
596 static void
597 tar_options(argc, argv)
598 register int argc;
599 register char **argv;
600 #endif
601 {
602 register int c;
603 int fstdin = 0;
604 int Oflag = 0;
605
606 /*
607 * Set default values.
608 */
609 rmleadslash = 1;
610
611 /*
612 * process option flags
613 */
614 while ((c = getoldopt(argc, argv,
615 "b:cef:hmopruts:vwxzBC:HLOPXZ014578"))
616 != EOF) {
617 switch(c) {
618 case 'b':
619 /*
620 * specify blocksize in 512-byte blocks
621 */
622 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
623 paxwarn(1, "Invalid block size %s", optarg);
624 tar_usage();
625 }
626 wrblksz *= 512; /* XXX - check for int oflow */
627 break;
628 case 'c':
629 /*
630 * create an archive
631 */
632 act = ARCHIVE;
633 break;
634 case 'e':
635 /*
636 * stop after first error
637 */
638 maxflt = 0;
639 break;
640 case 'f':
641 /*
642 * filename where the archive is stored
643 */
644 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
645 /*
646 * treat a - as stdin
647 */
648 fstdin = 1;
649 arcname = NULL;
650 break;
651 }
652 fstdin = 0;
653 arcname = optarg;
654 break;
655 case 'h':
656 /*
657 * follow symlinks
658 */
659 Lflag = 1;
660 break;
661 case 'm':
662 /*
663 * do not preserve modification time
664 */
665 pmtime = 0;
666 break;
667 case 'o':
668 if (opt_add("write_opt=nodir") < 0)
669 tar_usage();
670 case 'O':
671 Oflag = 1;
672 break;
673 case 'p':
674 /*
675 * preserve user id, group id, file
676 * mode, access/modification times
677 */
678 pids = 1;
679 pmode = 1;
680 patime = 1;
681 pmtime = 1;
682 break;
683 case 'r':
684 case 'u':
685 /*
686 * append to the archive
687 */
688 act = APPND;
689 break;
690 case 's':
691 /*
692 * file name substitution name pattern
693 */
694 if (rep_add(optarg) < 0) {
695 tar_usage();
696 break;
697 }
698 break;
699 case 't':
700 /*
701 * list contents of the tape
702 */
703 act = LIST;
704 break;
705 case 'v':
706 /*
707 * verbose operation mode
708 */
709 vflag++;
710 break;
711 case 'w':
712 /*
713 * interactive file rename
714 */
715 iflag = 1;
716 break;
717 case 'x':
718 /*
719 * write an archive, preserve ids if root
720 */
721 act = EXTRACT;
722 if (geteuid() == 0)
723 pids = 1;
724 break;
725 case 'z':
726 /*
727 * use gzip. Non standard option.
728 */
729 zflag = 1;
730 gzip_program = GZIP_CMD;
731 break;
732 case 'B':
733 /*
734 * Nothing to do here, this is pax default
735 */
736 break;
737 case 'C':
738 chdname = optarg;
739 break;
740 case 'H':
741 /*
742 * follow command line symlinks only
743 */
744 Hflag = 1;
745 break;
746 case 'L':
747 /*
748 * follow symlinks
749 */
750 Lflag = 1;
751 break;
752 case 'P':
753 /*
754 * do not remove leading '/' from pathnames
755 */
756 rmleadslash = 0;
757 break;
758 case 'X':
759 /*
760 * do not pass over mount points in the file system
761 */
762 Xflag = 1;
763 break;
764 case 'Z':
765 /*
766 * use compress.
767 */
768 zflag = 1;
769 gzip_program = COMPRESS_CMD;
770 break;
771 case '0':
772 arcname = DEV_0;
773 break;
774 case '1':
775 arcname = DEV_1;
776 break;
777 case '4':
778 arcname = DEV_4;
779 break;
780 case '5':
781 arcname = DEV_5;
782 break;
783 case '7':
784 arcname = DEV_7;
785 break;
786 case '8':
787 arcname = DEV_8;
788 break;
789 default:
790 tar_usage();
791 break;
792 }
793 }
794 argc -= optind;
795 argv += optind;
796
797 /* Traditional tar behaviour (pax wants to read filelist from stdin) */
798 if ((act == ARCHIVE || act == APPND) && argc == 0)
799 exit(0);
800
801 /*
802 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
803 * (unless -o specified)
804 */
805 if (act == ARCHIVE || act == APPND)
806 frmt = &(fsub[Oflag ? F_OTAR : F_TAR]);
807 else if (Oflag) {
808 paxwarn(1, "The -O/-o options are only valid when writing an archive");
809 tar_usage(); /* only valid when writing */
810 }
811
812 /*
813 * process the args as they are interpreted by the operation mode
814 */
815 switch (act) {
816 case LIST:
817 case EXTRACT:
818 default:
819 {
820 int sawpat = 0;
821
822 while (*argv != NULL) {
823 if (strcmp(*argv, "-C") == 0) {
824 if(*++argv == NULL)
825 break;
826 chdname = *argv++;
827
828 continue;
829 }
830 if (pat_add(*argv++, chdname) < 0)
831 tar_usage();
832 sawpat++;
833 }
834 /*
835 * if patterns were added, we are doing chdir()
836 * on a file-by-file basis, else, just one
837 * global chdir (if any) after opening input.
838 */
839 if (sawpat > 0)
840 chdname = NULL;
841 }
842 break;
843 case ARCHIVE:
844 case APPND:
845 if (chdname != NULL) { /* initial chdir() */
846 if (ftree_add(chdname, 1) < 0)
847 tar_usage();
848 }
849
850 while (*argv != NULL) {
851 if (!strcmp(*argv, "-C")) {
852 if (*++argv == NULL)
853 break;
854 if (ftree_add(*argv++, 1) < 0)
855 tar_usage();
856 } else {
857 if (ftree_add(*argv++, 0) < 0)
858 tar_usage();
859 }
860 }
861 /*
862 * no read errors allowed on updates/append operation!
863 */
864 maxflt = 0;
865 break;
866 }
867 if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
868 arcname = getenv("TAPE");
869 if ((arcname == NULL) || (*arcname == '\0'))
870 arcname = _PATH_DEFTAPE;
871 }
872 }
873
874 int
875 mkpath(path)
876 char *path;
877 {
878 struct stat sb;
879 register char *slash;
880 int done = 0;
881
882 slash = path;
883
884 while (!done) {
885 slash += strspn(slash, "/");
886 slash += strcspn(slash, "/");
887
888 done = (*slash == '\0');
889 *slash = '\0';
890
891 if (stat(path, &sb)) {
892 if (errno != ENOENT || mkdir(path, 0777)) {
893 paxwarn(1, "%s", path);
894 return (-1);
895 }
896 } else if (!S_ISDIR(sb.st_mode)) {
897 syswarn(1, ENOTDIR, "%s", path);
898 return (-1);
899 }
900
901 if (!done)
902 *slash = '/';
903 }
904
905 return (0);
906 }
907 /*
908 * cpio_options()
909 * look at the user specified flags. set globals as required and check if
910 * the user specified a legal set of flags. If not, complain and exit
911 */
912
913 #ifdef __STDC__
914 static void
915 cpio_options(register int argc, register char **argv)
916 #else
917 static void
918 cpio_options(argc, argv)
919 register int argc;
920 register char **argv;
921 #endif
922 {
923 register int c, i;
924 size_t len;
925 char *str;
926 FSUB tmp;
927 FILE *fp;
928
929 kflag = 1;
930 pids = 1;
931 pmode = 1;
932 pmtime = 0;
933 arcname = NULL;
934 dflag = 1;
935 act = -1;
936 nodirs = 1;
937 while ((c=getopt(argc,argv,"abcdfiklmoprstuvzABC:E:F:H:I:LO:SZ6")) != EOF)
938 switch (c) {
939 case 'a':
940 /*
941 * preserve access time on files read
942 */
943 tflag = 1;
944 break;
945 case 'b':
946 /*
947 * swap bytes and half-words when reading data
948 */
949 break;
950 case 'c':
951 /*
952 * ASCII cpio header
953 */
954 frmt = &(fsub[F_ACPIO]);
955 break;
956 case 'd':
957 /*
958 * create directories as needed
959 */
960 nodirs = 0;
961 break;
962 case 'f':
963 /*
964 * invert meaning of pattern list
965 */
966 cflag = 1;
967 break;
968 case 'i':
969 /*
970 * restore an archive
971 */
972 act = EXTRACT;
973 break;
974 case 'k':
975 break;
976 case 'l':
977 /*
978 * use links instead of copies when possible
979 */
980 lflag = 1;
981 break;
982 case 'm':
983 /*
984 * preserve modification time
985 */
986 pmtime = 1;
987 break;
988 case 'o':
989 /*
990 * create an archive
991 */
992 act = ARCHIVE;
993 frmt = &(fsub[F_CPIO]);
994 break;
995 case 'p':
996 /*
997 * copy-pass mode
998 */
999 act = COPY;
1000 break;
1001 case 'r':
1002 /*
1003 * interactively rename files
1004 */
1005 iflag = 1;
1006 break;
1007 case 's':
1008 /*
1009 * swap bytes after reading data
1010 */
1011 break;
1012 case 't':
1013 /*
1014 * list contents of archive
1015 */
1016 act = LIST;
1017 break;
1018 case 'u':
1019 /*
1020 * replace newer files
1021 */
1022 kflag = 0;
1023 break;
1024 case 'v':
1025 /*
1026 * verbose operation mode
1027 */
1028 vflag = 1;
1029 break;
1030 case 'z':
1031 /*
1032 * use gzip. Non standard option.
1033 */
1034 zflag = 1;
1035 gzip_program = GZIP_CMD;
1036 break;
1037 case 'A':
1038 /*
1039 * append mode
1040 */
1041 act = APPND;
1042 break;
1043 case 'B':
1044 /*
1045 * Use 5120 byte block size
1046 */
1047 wrblksz = 5120;
1048 break;
1049 case 'C':
1050 /*
1051 * set block size in bytes
1052 */
1053 wrblksz = atoi(optarg);
1054 break;
1055 case 'E':
1056 /*
1057 * file with patterns to extract or list
1058 */
1059 if ((fp = fopen(optarg, "r")) == NULL) {
1060 paxwarn(1, "Unable to open file '%s' for read", optarg);
1061 cpio_usage();
1062 }
1063 while ((str = fgetln(fp, &len)) != NULL) {
1064 str[len - 1] = '\0';
1065 pat_add(str, NULL);
1066 }
1067 fclose(fp);
1068 break;
1069 case 'F':
1070 case 'I':
1071 case 'O':
1072 /*
1073 * filename where the archive is stored
1074 */
1075 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1076 /*
1077 * treat a - as stdin
1078 */
1079 arcname = NULL;
1080 break;
1081 }
1082 arcname = optarg;
1083 break;
1084 case 'H':
1085 /*
1086 * specify an archive format on write
1087 */
1088 tmp.name = optarg;
1089 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1090 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL)
1091 break;
1092 paxwarn(1, "Unknown -H format: %s", optarg);
1093 (void)fputs("cpio: Known -H formats are:", stderr);
1094 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1095 (void)fprintf(stderr, " %s", fsub[i].name);
1096 (void)fputs("\n\n", stderr);
1097 cpio_usage();
1098 break;
1099 case 'L':
1100 /*
1101 * follow symbolic links
1102 */
1103 Lflag = 1;
1104 break;
1105 case 'S':
1106 /*
1107 * swap halfwords after reading data
1108 */
1109 break;
1110 case 'Z':
1111 /*
1112 * use compress. Non standard option.
1113 */
1114 zflag = 1;
1115 gzip_program = COMPRESS_CMD;
1116 break;
1117 case '6':
1118 /*
1119 * process Version 6 cpio format
1120 */
1121 frmt = &(fsub[F_OCPIO]);
1122 break;
1123 case '?':
1124 default:
1125 cpio_usage();
1126 break;
1127 }
1128 argc -= optind;
1129 argv += optind;
1130
1131 /*
1132 * process the args as they are interpreted by the operation mode
1133 */
1134 switch (act) {
1135 case LIST:
1136 case EXTRACT:
1137 while (*argv != NULL)
1138 if (pat_add(*argv++, NULL) < 0)
1139 cpio_usage();
1140 break;
1141 case COPY:
1142 if (*argv == NULL) {
1143 paxwarn(0, "Destination directory was not supplied");
1144 cpio_usage();
1145 }
1146 dirptr = *argv;
1147 if (mkpath(dirptr) < 0)
1148 cpio_usage();
1149 --argc;
1150 ++argv;
1151 /* FALL THROUGH */
1152 case ARCHIVE:
1153 case APPND:
1154 if (*argv != NULL)
1155 cpio_usage();
1156 /*
1157 * no read errors allowed on updates/append operation!
1158 */
1159 maxflt = 0;
1160 while ((str = fgetln(stdin, &len)) != NULL) {
1161 str[len - 1] = '\0';
1162 ftree_add(strdup(str), NULL);
1163 }
1164 break;
1165 default:
1166 cpio_usage();
1167 break;
1168 }
1169 }
1170
1171 /*
1172 * printflg()
1173 * print out those invalid flag sets found to the user
1174 */
1175
1176 #ifdef __STDC__
1177 static void
1178 printflg(unsigned int flg)
1179 #else
1180 static void
1181 printflg(flg)
1182 unsigned int flg;
1183 #endif
1184 {
1185 int nxt;
1186 int pos = 0;
1187
1188 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1189 while ((nxt = ffs(flg)) != 0) {
1190 flg = flg >> nxt;
1191 pos += nxt;
1192 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1193 }
1194 (void)putc('\n', stderr);
1195 }
1196
1197 /*
1198 * c_frmt()
1199 * comparison routine used by bsearch to find the format specified
1200 * by the user
1201 */
1202
1203 #ifdef __STDC__
1204 static int
1205 c_frmt(const void *a, const void *b)
1206 #else
1207 static int
1208 c_frmt(a, b)
1209 void *a;
1210 void *b;
1211 #endif
1212 {
1213 return(strcmp(((FSUB *)a)->name, ((FSUB *)b)->name));
1214 }
1215
1216 /*
1217 * opt_next()
1218 * called by format specific options routines to get each format specific
1219 * flag and value specified with -o
1220 * Return:
1221 * pointer to next OPLIST entry or NULL (end of list).
1222 */
1223
1224 #ifdef __STDC__
1225 OPLIST *
1226 opt_next(void)
1227 #else
1228 OPLIST *
1229 opt_next()
1230 #endif
1231 {
1232 OPLIST *opt;
1233
1234 if ((opt = ophead) != NULL)
1235 ophead = ophead->fow;
1236 return(opt);
1237 }
1238
1239 /*
1240 * bad_opt()
1241 * generic routine used to complain about a format specific options
1242 * when the format does not support options.
1243 */
1244
1245 #ifdef __STDC__
1246 int
1247 bad_opt(void)
1248 #else
1249 int
1250 bad_opt()
1251 #endif
1252 {
1253 register OPLIST *opt;
1254
1255 if (ophead == NULL)
1256 return(0);
1257 /*
1258 * print all we were given
1259 */
1260 paxwarn(1,"These format options are not supported");
1261 while ((opt = opt_next()) != NULL)
1262 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1263 pax_usage();
1264 return(0);
1265 }
1266
1267 /*
1268 * opt_add()
1269 * breaks the value supplied to -o into a option name and value. options
1270 * are given to -o in the form -o name-value,name=value
1271 * mulltiple -o may be specified.
1272 * Return:
1273 * 0 if format in name=value format, -1 if -o is passed junk
1274 */
1275
1276 #ifdef __STDC__
1277 int
1278 opt_add(register char *str)
1279 #else
1280 int
1281 opt_add(str)
1282 register char *str;
1283 #endif
1284 {
1285 register OPLIST *opt;
1286 register char *frpt;
1287 register char *pt;
1288 register char *endpt;
1289
1290 if ((str == NULL) || (*str == '\0')) {
1291 paxwarn(0, "Invalid option name");
1292 return(-1);
1293 }
1294 if ((str = strdup(str)) == NULL) {
1295 paxwarn(0, "Unable to allocate space for option list");
1296 return(-1);
1297 }
1298 frpt = endpt = str;
1299
1300 /*
1301 * break into name and values pieces and stuff each one into a
1302 * OPLIST structure. When we know the format, the format specific
1303 * option function will go through this list
1304 */
1305 while ((frpt != NULL) && (*frpt != '\0')) {
1306 if ((endpt = strchr(frpt, ',')) != NULL)
1307 *endpt = '\0';
1308 if ((pt = strchr(frpt, '=')) == NULL) {
1309 paxwarn(0, "Invalid options format");
1310 free(str);
1311 return(-1);
1312 }
1313 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1314 paxwarn(0, "Unable to allocate space for option list");
1315 free(str);
1316 return(-1);
1317 }
1318 *pt++ = '\0';
1319 opt->name = frpt;
1320 opt->value = pt;
1321 opt->fow = NULL;
1322 if (endpt != NULL)
1323 frpt = endpt + 1;
1324 else
1325 frpt = NULL;
1326 if (ophead == NULL) {
1327 optail = ophead = opt;
1328 continue;
1329 }
1330 optail->fow = opt;
1331 optail = opt;
1332 }
1333 return(0);
1334 }
1335
1336 /*
1337 * str_offt()
1338 * Convert an expression of the following forms to an off_t > 0.
1339 * 1) A positive decimal number.
1340 * 2) A positive decimal number followed by a b (mult by 512).
1341 * 3) A positive decimal number followed by a k (mult by 1024).
1342 * 4) A positive decimal number followed by a m (mult by 512).
1343 * 5) A positive decimal number followed by a w (mult by sizeof int)
1344 * 6) Two or more positive decimal numbers (with/without k,b or w).
1345 * seperated by x (also * for backwards compatibility), specifying
1346 * the product of the indicated values.
1347 * Return:
1348 * 0 for an error, a positive value o.w.
1349 */
1350
1351 #ifdef __STDC__
1352 static off_t
1353 str_offt(char *val)
1354 #else
1355 static off_t
1356 str_offt(val)
1357 char *val;
1358 #endif
1359 {
1360 char *expr;
1361 off_t num, t;
1362
1363 # ifdef NET2_STAT
1364 num = strtol(val, &expr, 0);
1365 if ((num == LONG_MAX) || (num <= 0) || (expr == val))
1366 # else
1367 num = strtoq(val, &expr, 0);
1368 if ((num == QUAD_MAX) || (num <= 0) || (expr == val))
1369 # endif
1370 return(0);
1371
1372 switch(*expr) {
1373 case 'b':
1374 t = num;
1375 num *= 512;
1376 if (t > num)
1377 return(0);
1378 ++expr;
1379 break;
1380 case 'k':
1381 t = num;
1382 num *= 1024;
1383 if (t > num)
1384 return(0);
1385 ++expr;
1386 break;
1387 case 'm':
1388 t = num;
1389 num *= 1048576;
1390 if (t > num)
1391 return(0);
1392 ++expr;
1393 break;
1394 case 'w':
1395 t = num;
1396 num *= sizeof(int);
1397 if (t > num)
1398 return(0);
1399 ++expr;
1400 break;
1401 }
1402
1403 switch(*expr) {
1404 case '\0':
1405 break;
1406 case '*':
1407 case 'x':
1408 t = num;
1409 num *= str_offt(expr + 1);
1410 if (t > num)
1411 return(0);
1412 break;
1413 default:
1414 return(0);
1415 }
1416 return(num);
1417 }
1418
1419 /*
1420 * no_op()
1421 * for those option functions where the archive format has nothing to do.
1422 * Return:
1423 * 0
1424 */
1425
1426 #ifdef __STDC__
1427 static int
1428 no_op(void)
1429 #else
1430 static int
1431 no_op()
1432 #endif
1433 {
1434 return(0);
1435 }
1436
1437 /*
1438 * pax_usage()
1439 * print the usage summary to the user
1440 */
1441
1442 #ifdef __STDC__
1443 void
1444 pax_usage(void)
1445 #else
1446 void
1447 pax_usage()
1448 #endif
1449 {
1450 (void)fputs("usage: pax [-cdnv] [-E limit] [-f archive] ", stderr);
1451 (void)fputs("[-s replstr] ... [-U user] ...", stderr);
1452 (void)fputs("\n [-G group] ... ", stderr);
1453 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1454 (void)fputs("[pattern ...]\n", stderr);
1455 (void)fputs(" pax -r [-cdiknuvDYZ] [-E limit] ", stderr);
1456 (void)fputs("[-f archive] [-o options] ... \n", stderr);
1457 (void)fputs(" [-p string] ... [-s replstr] ... ", stderr);
1458 (void)fputs("[-U user] ... [-G group] ...\n ", stderr);
1459 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1460 (void)fputs(" [pattern ...]\n", stderr);
1461 (void)fputs(" pax -w [-dituvHLPX] [-b blocksize] ", stderr);
1462 (void)fputs("[ [-a] [-f archive] ] [-x format] \n", stderr);
1463 (void)fputs(" [-B bytes] [-s replstr] ... ", stderr);
1464 (void)fputs("[-o options] ... [-U user] ...", stderr);
1465 (void)fputs("\n [-G group] ... ", stderr);
1466 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1467 (void)fputs("[file ...]\n", stderr);
1468 (void)fputs(" pax -r -w [-diklntuvDHLPXYZ] ", stderr);
1469 (void)fputs("[-p string] ... [-s replstr] ...", stderr);
1470 (void)fputs("\n [-U user] ... [-G group] ... ", stderr);
1471 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1472 (void)fputs("\n [file ...] directory\n", stderr);
1473 exit(1);
1474 }
1475
1476 /*
1477 * tar_usage()
1478 * print the usage summary to the user
1479 */
1480
1481 #ifdef __STDC__
1482 void
1483 tar_usage(void)
1484 #else
1485 void
1486 tar_usage()
1487 #endif
1488 {
1489 (void)fputs("usage: tar -{txru}[cevfbmopswzBHLPXZ014578] [tapefile] ",
1490 stderr);
1491 (void)fputs("[blocksize] [replstr] [-C directory] file1 file2...\n",
1492 stderr);
1493 exit(1);
1494 }
1495
1496 /*
1497 * cpio_usage()
1498 * print the usage summary to the user
1499 */
1500
1501 #ifdef __STDC__
1502 void
1503 cpio_usage(void)
1504 #else
1505 void
1506 cpio_usage()
1507 #endif
1508 {
1509 (void)fputs("usage: cpio -o [-aABcLvVzZ] [-C bytes] [-H format] [-O archive]\n", stderr);
1510 (void)fputs(" [-F archive] < name-list [> archive]\n", stderr);
1511 (void)fputs(" cpio -i [-bBcdfmnrsStuvVzZ6] [-C bytes] [-E file] [-H format]\n", stderr);
1512 (void)fputs(" [-I archive] [-F archive] [pattern...] [< archive]\n", stderr);
1513 (void)fputs(" cpio -p [-adlLmuvV] destination-directory < name-list\n", stderr);
1514 exit(1);
1515 }