Deleted Added
full compact
options.c (76286) options.c (76351)
1/*-
1/*
2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
8 *
9 * Redistribution and use in source and binary forms, with or without

--- 25 unchanged lines hidden (view full) ---

35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 4/18/94";
41#endif
42static const char rcsid[] =
2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
8 *
9 * Redistribution and use in source and binary forms, with or without

--- 25 unchanged lines hidden (view full) ---

35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 4/18/94";
41#endif
42static const char rcsid[] =
43 "$FreeBSD: head/bin/pax/options.c 76286 2001-05-05 01:10:13Z kris $";
43 "$FreeBSD: head/bin/pax/options.c 76351 2001-05-08 06:19:06Z kris $";
44#endif /* not lint */
45
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <sys/mtio.h>
49#include <stdio.h>
50#include <string.h>
44#endif /* not lint */
45
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <sys/mtio.h>
49#include <stdio.h>
50#include <string.h>
51#include <errno.h>
51#include <unistd.h>
52#include <stdlib.h>
53#include <limits.h>
52#include <unistd.h>
53#include <stdlib.h>
54#include <limits.h>
55#include <paths.h>
54#include "pax.h"
55#include "options.h"
56#include "cpio.h"
57#include "tar.h"
58#include "extern.h"
59
60/*
61 * Routines which handle command line options
62 */
63
64static char flgch[] = FLGCH; /* list of all possible flags */
65static OPLIST *ophead = NULL; /* head for format specific options -x */
66static OPLIST *optail = NULL; /* option tail */
67
68static int no_op __P((void));
69static void printflg __P((unsigned int));
70static int c_frmt __P((const void *, const void *));
71static off_t str_offt __P((char *));
56#include "pax.h"
57#include "options.h"
58#include "cpio.h"
59#include "tar.h"
60#include "extern.h"
61
62/*
63 * Routines which handle command line options
64 */
65
66static char flgch[] = FLGCH; /* list of all possible flags */
67static OPLIST *ophead = NULL; /* head for format specific options -x */
68static OPLIST *optail = NULL; /* option tail */
69
70static int no_op __P((void));
71static void printflg __P((unsigned int));
72static int c_frmt __P((const void *, const void *));
73static off_t str_offt __P((char *));
74static char *getline __P((FILE *fp));
72static void pax_options __P((register int, register char **));
73static void pax_usage __P((void));
74static void tar_options __P((register int, register char **));
75static void tar_usage __P((void));
75static void pax_options __P((register int, register char **));
76static void pax_usage __P((void));
77static void tar_options __P((register int, register char **));
78static void tar_usage __P((void));
76#ifdef notdef
77static void cpio_options __P((register int, register char **));
78static void cpio_usage __P((void));
79static void cpio_options __P((register int, register char **));
80static void cpio_usage __P((void));
79#endif
80
81
82/* errors from getline */
83#define GETLINE_FILE_CORRUPT 1
84#define GETLINE_OUT_OF_MEM 2
85static int getline_error;
86
87
81#define GZIP_CMD "gzip" /* command to run as gzip */
82#define COMPRESS_CMD "compress" /* command to run as compress */
88#define GZIP_CMD "gzip" /* command to run as gzip */
89#define COMPRESS_CMD "compress" /* command to run as compress */
90#define BZIP2_CMD "bzip2" /* command to run as gzip */
83
84/*
85 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
86 * (see pax.h for description of each function)
87 *
88 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
89 * read, end_read, st_write, write, end_write, trail,
90 * rd_data, wr_data, options

--- 25 unchanged lines hidden (view full) ---

116 tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
117 rd_wrfile, wr_rdfile, tar_opt},
118
119/* 5: POSIX USTAR */
120 {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
121 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
122 rd_wrfile, wr_rdfile, bad_opt},
123};
91
92/*
93 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
94 * (see pax.h for description of each function)
95 *
96 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
97 * read, end_read, st_write, write, end_write, trail,
98 * rd_data, wr_data, options

--- 25 unchanged lines hidden (view full) ---

124 tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
125 rd_wrfile, wr_rdfile, tar_opt},
126
127/* 5: POSIX USTAR */
128 {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
129 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
130 rd_wrfile, wr_rdfile, bad_opt},
131};
124#define F_TAR 4 /* format when called as tar */
132#define F_OCPIO 0 /* format when called as cpio -6 */
133#define F_ACPIO 1 /* format when called as cpio -c */
134#define F_CPIO 3 /* format when called as cpio */
135#define F_OTAR 4 /* format when called as tar -o */
136#define F_TAR 5 /* format when called as tar */
125#define DEFLT 5 /* default write format from list above */
126
127/*
128 * ford is the archive search order used by get_arc() to determine what kind
129 * of archive we are dealing with. This helps to properly id archive formats
130 * some formats may be subsets of others....
131 */
132int ford[] = {5, 4, 3, 2, 1, 0, -1 };

--- 20 unchanged lines hidden (view full) ---

153 */
154 if ((argv0 = strrchr(argv[0], '/')) != NULL)
155 argv0++;
156 else
157 argv0 = argv[0];
158
159 if (strcmp(NM_TAR, argv0) == 0)
160 return(tar_options(argc, argv));
137#define DEFLT 5 /* default write format from list above */
138
139/*
140 * ford is the archive search order used by get_arc() to determine what kind
141 * of archive we are dealing with. This helps to properly id archive formats
142 * some formats may be subsets of others....
143 */
144int ford[] = {5, 4, 3, 2, 1, 0, -1 };

--- 20 unchanged lines hidden (view full) ---

165 */
166 if ((argv0 = strrchr(argv[0], '/')) != NULL)
167 argv0++;
168 else
169 argv0 = argv[0];
170
171 if (strcmp(NM_TAR, argv0) == 0)
172 return(tar_options(argc, argv));
161# ifdef notdef
162 else if (strcmp(NM_CPIO, argv0) == 0)
163 return(cpio_options(argc, argv));
173 else if (strcmp(NM_CPIO, argv0) == 0)
174 return(cpio_options(argc, argv));
164# endif
165 /*
166 * assume pax as the default
167 */
168 argv0 = NM_PAX;
169 return(pax_options(argc, argv));
170}
171
172/*

--- 190 unchanged lines hidden (view full) ---

363 flg |= WF;
364 break;
365 case 'x':
366 /*
367 * specify an archive format on write
368 */
369 tmp.name = optarg;
370 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
175 /*
176 * assume pax as the default
177 */
178 argv0 = NM_PAX;
179 return(pax_options(argc, argv));
180}
181
182/*

--- 190 unchanged lines hidden (view full) ---

373 flg |= WF;
374 break;
375 case 'x':
376 /*
377 * specify an archive format on write
378 */
379 tmp.name = optarg;
380 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
371 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt))) {
381 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL) {
372 flg |= XF;
373 break;
374 }
375 paxwarn(1, "Unknown -x format: %s", optarg);
376 (void)fputs("pax: Known -x formats are:", stderr);
377 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
378 (void)fprintf(stderr, " %s", fsub[i].name);
379 (void)fputs("\n\n", stderr);

--- 115 unchanged lines hidden (view full) ---

495 case 'Z':
496 /*
497 * On extraction check modification time after the
498 * modification of the file name. Non standard option.
499 */
500 Zflag = 1;
501 flg |= CZF;
502 break;
382 flg |= XF;
383 break;
384 }
385 paxwarn(1, "Unknown -x format: %s", optarg);
386 (void)fputs("pax: Known -x formats are:", stderr);
387 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
388 (void)fprintf(stderr, " %s", fsub[i].name);
389 (void)fputs("\n\n", stderr);

--- 115 unchanged lines hidden (view full) ---

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;
503 case '?':
504 default:
505 pax_usage();
506 break;
507 }
508 }
509
510 /*
511 * figure out the operation mode of pax read,write,extract,copy,append
512 * or list. check that we have not been given a bogus set of flags
513 * for the operation mode.
514 */
515 if (ISLIST(flg)) {
516 act = LIST;
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 listf = stdout;
517 bflg = flg & BDLIST;
518 } else if (ISEXTRACT(flg)) {
519 act = EXTRACT;
520 bflg = flg & BDEXTR;
521 } else if (ISARCHIVE(flg)) {
522 act = ARCHIVE;
523 bflg = flg & BDARCH;
524 } else if (ISAPPND(flg)) {

--- 19 unchanged lines hidden (view full) ---

544
545 /*
546 * process the args as they are interpreted by the operation mode
547 */
548 switch (act) {
549 case LIST:
550 case EXTRACT:
551 for (; optind < argc; optind++)
527 bflg = flg & BDLIST;
528 } else if (ISEXTRACT(flg)) {
529 act = EXTRACT;
530 bflg = flg & BDEXTR;
531 } else if (ISARCHIVE(flg)) {
532 act = ARCHIVE;
533 bflg = flg & BDARCH;
534 } else if (ISAPPND(flg)) {

--- 19 unchanged lines hidden (view full) ---

554
555 /*
556 * process the args as they are interpreted by the operation mode
557 */
558 switch (act) {
559 case LIST:
560 case EXTRACT:
561 for (; optind < argc; optind++)
552 if (pat_add(argv[optind]) < 0)
562 if (pat_add(argv[optind], NULL) < 0)
553 pax_usage();
554 break;
555 case COPY:
556 if (optind >= argc) {
557 paxwarn(0, "Destination directory was not supplied");
558 pax_usage();
559 }
560 --argc;
561 dirptr = argv[argc];
562 /* FALL THROUGH */
563 case ARCHIVE:
564 case APPND:
565 for (; optind < argc; optind++)
563 pax_usage();
564 break;
565 case COPY:
566 if (optind >= argc) {
567 paxwarn(0, "Destination directory was not supplied");
568 pax_usage();
569 }
570 --argc;
571 dirptr = argv[argc];
572 /* FALL THROUGH */
573 case ARCHIVE:
574 case APPND:
575 for (; optind < argc; optind++)
566 if (ftree_add(argv[optind]) < 0)
576 if (ftree_add(argv[optind], 0) < 0)
567 pax_usage();
568 /*
569 * no read errors allowed on updates/append operation!
570 */
571 maxflt = 0;
572 break;
573 }
574}

--- 10 unchanged lines hidden (view full) ---

585tar_options(register int argc, register char **argv)
586#else
587static void
588tar_options(argc, argv)
589 register int argc;
590 register char **argv;
591#endif
592{
577 pax_usage();
578 /*
579 * no read errors allowed on updates/append operation!
580 */
581 maxflt = 0;
582 break;
583 }
584}

--- 10 unchanged lines hidden (view full) ---

595tar_options(register int argc, register char **argv)
596#else
597static void
598tar_options(argc, argv)
599 register int argc;
600 register char **argv;
601#endif
602{
593 register char *cp;
603 register int c;
594 int fstdin = 0;
604 int fstdin = 0;
605 int Oflag = 0;
606 int nincfiles = 0;
607 int incfiles_max = 0;
608 struct incfile {
609 char *file;
610 char *dir;
611 };
612 struct incfile *incfiles = NULL;
595
613
596 if (argc < 2)
597 tar_usage();
598 /*
614 /*
615 * Set default values.
616 */
617 rmleadslash = 1;
618
619 /*
599 * process option flags
600 */
620 * process option flags
621 */
601 ++argv;
602 for (cp = *argv++; *cp != '\0'; ++cp) {
603 switch (*cp) {
604 case '-':
605 /*
606 * skip over -
607 */
608 break;
622 while ((c = getoldopt(argc, argv,
623 "b:cef:hjmopqruts:vwxyzBC:HI:LOPXZ014578")) != -1) {
624 switch(c) {
609 case 'b':
610 /*
625 case 'b':
626 /*
611 * specify blocksize
627 * specify blocksize in 512-byte blocks
612 */
628 */
613 if (*argv == NULL) {
614 paxwarn(1,"blocksize must be specified with 'b'");
629 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
630 paxwarn(1, "Invalid block size %s", optarg);
615 tar_usage();
616 }
631 tar_usage();
632 }
617 if ((wrblksz = (int)str_offt(*argv)) <= 0) {
618 paxwarn(1, "Invalid block size %s", *argv);
619 tar_usage();
620 }
621 ++argv;
633 wrblksz *= 512; /* XXX - check for int oflow */
622 break;
623 case 'c':
624 /*
625 * create an archive
626 */
627 act = ARCHIVE;
628 break;
629 case 'e':
630 /*
631 * stop after first error
632 */
633 maxflt = 0;
634 break;
635 case 'f':
636 /*
637 * filename where the archive is stored
638 */
634 break;
635 case 'c':
636 /*
637 * create an archive
638 */
639 act = ARCHIVE;
640 break;
641 case 'e':
642 /*
643 * stop after first error
644 */
645 maxflt = 0;
646 break;
647 case 'f':
648 /*
649 * filename where the archive is stored
650 */
639 if (*argv == NULL) {
640 paxwarn(1, "filename must be specified with 'f'");
641 tar_usage();
642 }
643 if ((argv[0][0] == '-') && (argv[0][1]== '\0')) {
651 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
644 /*
645 * treat a - as stdin
646 */
652 /*
653 * treat a - as stdin
654 */
647 ++argv;
648 ++fstdin;
649 arcname = (char *)0;
655 fstdin = 1;
656 arcname = NULL;
650 break;
651 }
652 fstdin = 0;
657 break;
658 }
659 fstdin = 0;
653 arcname = *argv++;
660 arcname = optarg;
654 break;
661 break;
662 case 'h':
663 /*
664 * follow symlinks
665 */
666 Lflag = 1;
667 break;
668 case 'j':
669 case 'y':
670 /*
671 * use bzip2. Non standard option.
672 */
673 gzip_program = BZIP2_CMD;
674 break;
655 case 'm':
656 /*
657 * do not preserve modification time
658 */
659 pmtime = 0;
660 break;
661 case 'o':
662 if (opt_add("write_opt=nodir") < 0)
663 tar_usage();
675 case 'm':
676 /*
677 * do not preserve modification time
678 */
679 pmtime = 0;
680 break;
681 case 'o':
682 if (opt_add("write_opt=nodir") < 0)
683 tar_usage();
684 case 'O':
685 Oflag = 1;
664 break;
665 case 'p':
666 /*
686 break;
687 case 'p':
688 /*
667 * preserve user id, group id, file
668 * mode, access/modification times
689 * preserve uid/gid and file mode, regardless of umask
669 */
690 */
670 pids = 1;
671 pmode = 1;
691 pmode = 1;
672 patime = 1;
673 pmtime = 1;
692 pids = 1;
674 break;
693 break;
694 case 'q':
695 /*
696 * select first match for a pattern only
697 */
698 nflag = 1;
699 break;
675 case 'r':
676 case 'u':
677 /*
678 * append to the archive
679 */
680 act = APPND;
681 break;
700 case 'r':
701 case 'u':
702 /*
703 * append to the archive
704 */
705 act = APPND;
706 break;
707 case 's':
708 /*
709 * file name substitution name pattern
710 */
711 if (rep_add(optarg) < 0) {
712 tar_usage();
713 break;
714 }
715 break;
682 case 't':
683 /*
684 * list contents of the tape
685 */
686 act = LIST;
687 break;
688 case 'v':
689 /*
690 * verbose operation mode
691 */
716 case 't':
717 /*
718 * list contents of the tape
719 */
720 act = LIST;
721 break;
722 case 'v':
723 /*
724 * verbose operation mode
725 */
692 vflag = 1;
726 vflag++;
693 break;
694 case 'w':
695 /*
696 * interactive file rename
697 */
698 iflag = 1;
699 break;
700 case 'x':
701 /*
727 break;
728 case 'w':
729 /*
730 * interactive file rename
731 */
732 iflag = 1;
733 break;
734 case 'x':
735 /*
702 * write an archive
736 * extract an archive, preserving mode,
737 * and mtime if possible.
703 */
704 act = EXTRACT;
738 */
739 act = EXTRACT;
740 pmtime = 1;
705 break;
706 case 'z':
707 /*
708 * use gzip. Non standard option.
709 */
710 gzip_program = GZIP_CMD;
711 break;
712 case 'B':
713 /*
714 * Nothing to do here, this is pax default
715 */
716 break;
741 break;
742 case 'z':
743 /*
744 * use gzip. Non standard option.
745 */
746 gzip_program = GZIP_CMD;
747 break;
748 case 'B':
749 /*
750 * Nothing to do here, this is pax default
751 */
752 break;
753 case 'C':
754 chdname = optarg;
755 break;
717 case 'H':
718 /*
719 * follow command line symlinks only
720 */
721 Hflag = 1;
722 break;
756 case 'H':
757 /*
758 * follow command line symlinks only
759 */
760 Hflag = 1;
761 break;
762 case 'I':
763 if (++nincfiles > incfiles_max) {
764 incfiles_max = nincfiles + 3;
765 incfiles = realloc(incfiles,
766 sizeof(*incfiles) * incfiles_max);
767 if (incfiles == NULL) {
768 paxwarn(0, "Unable to allocate space "
769 "for option list");
770 exit(1);
771 }
772 }
773 incfiles[nincfiles - 1].file = optarg;
774 incfiles[nincfiles - 1].dir = chdname;
775 break;
723 case 'L':
724 /*
725 * follow symlinks
726 */
727 Lflag = 1;
728 break;
729 case 'P':
730 /*
776 case 'L':
777 /*
778 * follow symlinks
779 */
780 Lflag = 1;
781 break;
782 case 'P':
783 /*
731 * do not follow symlinks
784 * do not remove leading '/' from pathnames
732 */
785 */
733 Lflag = 0;
786 rmleadslash = 0;
734 break;
735 case 'X':
736 /*
737 * do not pass over mount points in the file system
738 */
739 Xflag = 1;
740 break;
741 case 'Z':

--- 20 unchanged lines hidden (view full) ---

762 case '8':
763 arcname = DEV_8;
764 break;
765 default:
766 tar_usage();
767 break;
768 }
769 }
787 break;
788 case 'X':
789 /*
790 * do not pass over mount points in the file system
791 */
792 Xflag = 1;
793 break;
794 case 'Z':

--- 20 unchanged lines hidden (view full) ---

815 case '8':
816 arcname = DEV_8;
817 break;
818 default:
819 tar_usage();
820 break;
821 }
822 }
823 argc -= optind;
824 argv += optind;
770
825
826 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
827 if (fstdin == 1 && act == ARCHIVE)
828 listf = stderr;
829 else
830 listf = stdout;
831
832 /* Traditional tar behaviour (pax wants to read file list from stdin) */
833 if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
834 exit(0);
835
771 /*
772 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
836 /*
837 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
838 * (unless -o specified)
773 */
839 */
774 if (act == ARCHIVE)
775 frmt = &(fsub[F_TAR]);
840 if (act == ARCHIVE || act == APPND)
841 frmt = &(fsub[Oflag ? F_OTAR : F_TAR]);
842 else if (Oflag) {
843 paxwarn(1, "The -O/-o options are only valid when writing an archive");
844 tar_usage(); /* only valid when writing */
845 }
776
777 /*
778 * process the args as they are interpreted by the operation mode
779 */
780 switch (act) {
781 case LIST:
782 case EXTRACT:
783 default:
846
847 /*
848 * process the args as they are interpreted by the operation mode
849 */
850 switch (act) {
851 case LIST:
852 case EXTRACT:
853 default:
784 while (*argv != NULL)
785 if (pat_add(*argv++) < 0)
786 tar_usage();
854 {
855 int sawpat = 0;
856 char *file, *dir = NULL;
857
858 while (nincfiles || *argv != NULL) {
859 /*
860 * If we queued up any include files,
861 * pull them in now. Otherwise, check
862 * for -I and -C positional flags.
863 * Anything else must be a file to
864 * extract.
865 */
866 if (nincfiles) {
867 file = incfiles->file;
868 dir = incfiles->dir;
869 incfiles++;
870 nincfiles--;
871 } else if (strcmp(*argv, "-I") == 0) {
872 if (*++argv == NULL)
873 break;
874 file = *argv++;
875 dir = chdname;
876 } else
877 file = NULL;
878 if (file != NULL) {
879 FILE *fp;
880 char *str;
881
882 if (strcmp(file, "-") == 0)
883 fp = stdin;
884 else if ((fp = fopen(file, "r")) == NULL) {
885 paxwarn(1, "Unable to open file '%s' for read", file);
886 tar_usage();
887 }
888 while ((str = getline(fp)) != NULL) {
889 if (pat_add(str, dir) < 0)
890 tar_usage();
891 sawpat = 1;
892 }
893 if (strcmp(file, "-") != 0)
894 fclose(fp);
895 if (getline_error) {
896 paxwarn(1, "Problem with file '%s'", file);
897 tar_usage();
898 }
899 } else if (strcmp(*argv, "-C") == 0) {
900 if (*++argv == NULL)
901 break;
902 chdname = *argv++;
903 } else if (pat_add(*argv++, chdname) < 0)
904 tar_usage();
905 else
906 sawpat = 1;
907 }
908 /*
909 * if patterns were added, we are doing chdir()
910 * on a file-by-file basis, else, just one
911 * global chdir (if any) after opening input.
912 */
913 if (sawpat > 0)
914 chdname = NULL;
915 }
787 break;
788 case ARCHIVE:
789 case APPND:
916 break;
917 case ARCHIVE:
918 case APPND:
790 while (*argv != NULL)
791 if (ftree_add(*argv++) < 0)
919 if (chdname != NULL) { /* initial chdir() */
920 if (ftree_add(chdname, 1) < 0)
792 tar_usage();
921 tar_usage();
922 }
923
924 while (nincfiles || *argv != NULL) {
925 char *file, *dir = NULL;
926
927 /*
928 * If we queued up any include files, pull them in
929 * now. Otherwise, check for -I and -C positional
930 * flags. Anything else must be a file to include
931 * in the archive.
932 */
933 if (nincfiles) {
934 file = incfiles->file;
935 dir = incfiles->dir;
936 incfiles++;
937 nincfiles--;
938 } else if (strcmp(*argv, "-I") == 0) {
939 if (*++argv == NULL)
940 break;
941 file = *argv++;
942 dir = NULL;
943 } else
944 file = NULL;
945 if (file != NULL) {
946 FILE *fp;
947 char *str;
948
949 /* Set directory if needed */
950 if (dir) {
951 if (ftree_add(dir, 1) < 0)
952 tar_usage();
953 }
954
955 if (strcmp(file, "-") == 0)
956 fp = stdin;
957 else if ((fp = fopen(file, "r")) == NULL) {
958 paxwarn(1, "Unable to open file '%s' for read", file);
959 tar_usage();
960 }
961 while ((str = getline(fp)) != NULL) {
962 if (ftree_add(str, 0) < 0)
963 tar_usage();
964 }
965 if (strcmp(file, "-") != 0)
966 fclose(fp);
967 if (getline_error) {
968 paxwarn(1, "Problem with file '%s'",
969 file);
970 tar_usage();
971 }
972 } else if (strcmp(*argv, "-C") == 0) {
973 if (*++argv == NULL)
974 break;
975 if (ftree_add(*argv++, 1) < 0)
976 tar_usage();
977 } else if (ftree_add(*argv++, 0) < 0)
978 tar_usage();
979 }
793 /*
794 * no read errors allowed on updates/append operation!
795 */
796 maxflt = 0;
797 break;
798 }
799 if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
800 arcname = getenv("TAPE");
801 if ((arcname == NULL) || (*arcname == '\0'))
980 /*
981 * no read errors allowed on updates/append operation!
982 */
983 maxflt = 0;
984 break;
985 }
986 if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
987 arcname = getenv("TAPE");
988 if ((arcname == NULL) || (*arcname == '\0'))
802 arcname = DEV_8;
989 arcname = _PATH_DEFTAPE;
803 }
804}
805
990 }
991}
992
806#ifdef notdef
993int
994mkpath(path)
995 char *path;
996{
997 struct stat sb;
998 register char *slash;
999 int done = 0;
1000
1001 slash = path;
1002
1003 while (!done) {
1004 slash += strspn(slash, "/");
1005 slash += strcspn(slash, "/");
1006
1007 done = (*slash == '\0');
1008 *slash = '\0';
1009
1010 if (stat(path, &sb)) {
1011 if (errno != ENOENT || mkdir(path, 0777)) {
1012 paxwarn(1, "%s", path);
1013 return (-1);
1014 }
1015 } else if (!S_ISDIR(sb.st_mode)) {
1016 syswarn(1, ENOTDIR, "%s", path);
1017 return (-1);
1018 }
1019
1020 if (!done)
1021 *slash = '/';
1022 }
1023
1024 return (0);
1025}
807/*
808 * cpio_options()
809 * look at the user specified flags. set globals as required and check if
810 * the user specified a legal set of flags. If not, complain and exit
811 */
812
813#ifdef __STDC__
814static void
815cpio_options(register int argc, register char **argv)
816#else
817static void
818cpio_options(argc, argv)
819 register int argc;
820 register char **argv;
821#endif
822{
1026/*
1027 * cpio_options()
1028 * look at the user specified flags. set globals as required and check if
1029 * the user specified a legal set of flags. If not, complain and exit
1030 */
1031
1032#ifdef __STDC__
1033static void
1034cpio_options(register int argc, register char **argv)
1035#else
1036static void
1037cpio_options(argc, argv)
1038 register int argc;
1039 register char **argv;
1040#endif
1041{
1042 register int c, i;
1043 char *str;
1044 FSUB tmp;
1045 FILE *fp;
1046
1047 kflag = 1;
1048 pids = 1;
1049 pmode = 1;
1050 pmtime = 0;
1051 arcname = NULL;
1052 dflag = 1;
1053 act = -1;
1054 nodirs = 1;
1055 while ((c=getopt(argc,argv,"abcdfiklmoprstuvzABC:E:F:H:I:LO:SZ6")) != -1)
1056 switch (c) {
1057 case 'a':
1058 /*
1059 * preserve access time on files read
1060 */
1061 tflag = 1;
1062 break;
1063 case 'b':
1064 /*
1065 * swap bytes and half-words when reading data
1066 */
1067 break;
1068 case 'c':
1069 /*
1070 * ASCII cpio header
1071 */
1072 frmt = &(fsub[F_ACPIO]);
1073 break;
1074 case 'd':
1075 /*
1076 * create directories as needed
1077 */
1078 nodirs = 0;
1079 break;
1080 case 'f':
1081 /*
1082 * invert meaning of pattern list
1083 */
1084 cflag = 1;
1085 break;
1086 case 'i':
1087 /*
1088 * restore an archive
1089 */
1090 act = EXTRACT;
1091 break;
1092 case 'k':
1093 break;
1094 case 'l':
1095 /*
1096 * use links instead of copies when possible
1097 */
1098 lflag = 1;
1099 break;
1100 case 'm':
1101 /*
1102 * preserve modification time
1103 */
1104 pmtime = 1;
1105 break;
1106 case 'o':
1107 /*
1108 * create an archive
1109 */
1110 act = ARCHIVE;
1111 frmt = &(fsub[F_CPIO]);
1112 break;
1113 case 'p':
1114 /*
1115 * copy-pass mode
1116 */
1117 act = COPY;
1118 break;
1119 case 'r':
1120 /*
1121 * interactively rename files
1122 */
1123 iflag = 1;
1124 break;
1125 case 's':
1126 /*
1127 * swap bytes after reading data
1128 */
1129 break;
1130 case 't':
1131 /*
1132 * list contents of archive
1133 */
1134 act = LIST;
1135 listf = stdout;
1136 break;
1137 case 'u':
1138 /*
1139 * replace newer files
1140 */
1141 kflag = 0;
1142 break;
1143 case 'v':
1144 /*
1145 * verbose operation mode
1146 */
1147 vflag = 1;
1148 break;
1149 case 'z':
1150 /*
1151 * use gzip. Non standard option.
1152 */
1153 gzip_program = GZIP_CMD;
1154 break;
1155 case 'A':
1156 /*
1157 * append mode
1158 */
1159 act = APPND;
1160 break;
1161 case 'B':
1162 /*
1163 * Use 5120 byte block size
1164 */
1165 wrblksz = 5120;
1166 break;
1167 case 'C':
1168 /*
1169 * set block size in bytes
1170 */
1171 wrblksz = atoi(optarg);
1172 break;
1173 case 'E':
1174 /*
1175 * file with patterns to extract or list
1176 */
1177 if ((fp = fopen(optarg, "r")) == NULL) {
1178 paxwarn(1, "Unable to open file '%s' for read", optarg);
1179 cpio_usage();
1180 }
1181 while ((str = getline(fp)) != NULL) {
1182 pat_add(str, NULL);
1183 }
1184 fclose(fp);
1185 if (getline_error) {
1186 paxwarn(1, "Problem with file '%s'", optarg);
1187 cpio_usage();
1188 }
1189 break;
1190 case 'F':
1191 case 'I':
1192 case 'O':
1193 /*
1194 * filename where the archive is stored
1195 */
1196 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1197 /*
1198 * treat a - as stdin
1199 */
1200 arcname = NULL;
1201 break;
1202 }
1203 arcname = optarg;
1204 break;
1205 case 'H':
1206 /*
1207 * specify an archive format on write
1208 */
1209 tmp.name = optarg;
1210 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1211 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL)
1212 break;
1213 paxwarn(1, "Unknown -H format: %s", optarg);
1214 (void)fputs("cpio: Known -H formats are:", stderr);
1215 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1216 (void)fprintf(stderr, " %s", fsub[i].name);
1217 (void)fputs("\n\n", stderr);
1218 cpio_usage();
1219 break;
1220 case 'L':
1221 /*
1222 * follow symbolic links
1223 */
1224 Lflag = 1;
1225 break;
1226 case 'S':
1227 /*
1228 * swap halfwords after reading data
1229 */
1230 break;
1231 case 'Z':
1232 /*
1233 * use compress. Non standard option.
1234 */
1235 gzip_program = COMPRESS_CMD;
1236 break;
1237 case '6':
1238 /*
1239 * process Version 6 cpio format
1240 */
1241 frmt = &(fsub[F_OCPIO]);
1242 break;
1243 case '?':
1244 default:
1245 cpio_usage();
1246 break;
1247 }
1248 argc -= optind;
1249 argv += optind;
1250
1251 /*
1252 * process the args as they are interpreted by the operation mode
1253 */
1254 switch (act) {
1255 case LIST:
1256 case EXTRACT:
1257 while (*argv != NULL)
1258 if (pat_add(*argv++, NULL) < 0)
1259 cpio_usage();
1260 break;
1261 case COPY:
1262 if (*argv == NULL) {
1263 paxwarn(0, "Destination directory was not supplied");
1264 cpio_usage();
1265 }
1266 dirptr = *argv;
1267 if (mkpath(dirptr) < 0)
1268 cpio_usage();
1269 --argc;
1270 ++argv;
1271 /* FALL THROUGH */
1272 case ARCHIVE:
1273 case APPND:
1274 if (*argv != NULL)
1275 cpio_usage();
1276 /*
1277 * no read errors allowed on updates/append operation!
1278 */
1279 maxflt = 0;
1280 while ((str = getline(stdin)) != NULL) {
1281 ftree_add(str, NULL);
1282 }
1283 if (getline_error) {
1284 paxwarn(1, "Problem while reading stdin");
1285 cpio_usage();
1286 }
1287 break;
1288 default:
1289 cpio_usage();
1290 break;
1291 }
823}
1292}
824#endif
825
826/*
827 * printflg()
828 * print out those invalid flag sets found to the user
829 */
830
831#ifdef __STDC__
832static void
833printflg(unsigned int flg)
834#else
835static void
836printflg(flg)
837 unsigned int flg;
838#endif
839{
840 int nxt;
841 int pos = 0;
842
843 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1293
1294/*
1295 * printflg()
1296 * print out those invalid flag sets found to the user
1297 */
1298
1299#ifdef __STDC__
1300static void
1301printflg(unsigned int flg)
1302#else
1303static void
1304printflg(flg)
1305 unsigned int flg;
1306#endif
1307{
1308 int nxt;
1309 int pos = 0;
1310
1311 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
844 while ((nxt = ffs(flg))) {
1312 while ((nxt = ffs(flg)) != 0) {
845 flg = flg >> nxt;
846 pos += nxt;
847 (void)fprintf(stderr, " -%c", flgch[pos-1]);
848 }
849 (void)putc('\n', stderr);
850}
851
852/*

--- 88 unchanged lines hidden (view full) ---

941 register char *frpt;
942 register char *pt;
943 register char *endpt;
944
945 if ((str == NULL) || (*str == '\0')) {
946 paxwarn(0, "Invalid option name");
947 return(-1);
948 }
1313 flg = flg >> nxt;
1314 pos += nxt;
1315 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1316 }
1317 (void)putc('\n', stderr);
1318}
1319
1320/*

--- 88 unchanged lines hidden (view full) ---

1409 register char *frpt;
1410 register char *pt;
1411 register char *endpt;
1412
1413 if ((str == NULL) || (*str == '\0')) {
1414 paxwarn(0, "Invalid option name");
1415 return(-1);
1416 }
1417 if ((str = strdup(str)) == NULL) {
1418 paxwarn(0, "Unable to allocate space for option list");
1419 return(-1);
1420 }
949 frpt = endpt = str;
950
951 /*
952 * break into name and values pieces and stuff each one into a
953 * OPLIST structure. When we know the format, the format specific
954 * option function will go through this list
955 */
956 while ((frpt != NULL) && (*frpt != '\0')) {
957 if ((endpt = strchr(frpt, ',')) != NULL)
958 *endpt = '\0';
959 if ((pt = strchr(frpt, '=')) == NULL) {
960 paxwarn(0, "Invalid options format");
1421 frpt = endpt = str;
1422
1423 /*
1424 * break into name and values pieces and stuff each one into a
1425 * OPLIST structure. When we know the format, the format specific
1426 * option function will go through this list
1427 */
1428 while ((frpt != NULL) && (*frpt != '\0')) {
1429 if ((endpt = strchr(frpt, ',')) != NULL)
1430 *endpt = '\0';
1431 if ((pt = strchr(frpt, '=')) == NULL) {
1432 paxwarn(0, "Invalid options format");
1433 free(str);
961 return(-1);
962 }
963 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
964 paxwarn(0, "Unable to allocate space for option list");
1434 return(-1);
1435 }
1436 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1437 paxwarn(0, "Unable to allocate space for option list");
1438 free(str);
965 return(-1);
966 }
967 *pt++ = '\0';
968 opt->name = frpt;
969 opt->value = pt;
970 opt->fow = NULL;
971 if (endpt != NULL)
972 frpt = endpt + 1;

--- 87 unchanged lines hidden (view full) ---

1060 return(0);
1061 break;
1062 default:
1063 return(0);
1064 }
1065 return(num);
1066}
1067
1439 return(-1);
1440 }
1441 *pt++ = '\0';
1442 opt->name = frpt;
1443 opt->value = pt;
1444 opt->fow = NULL;
1445 if (endpt != NULL)
1446 frpt = endpt + 1;

--- 87 unchanged lines hidden (view full) ---

1534 return(0);
1535 break;
1536 default:
1537 return(0);
1538 }
1539 return(num);
1540}
1541
1542#ifdef __STDC__
1543char *
1544getline(FILE *f)
1545#else
1546char *
1547getline(f)
1548 FILE *f;
1549#endif
1550{
1551 char *name, *temp;
1552 size_t len;
1553
1554 name = fgetln(f, &len);
1555 if (!name) {
1556 getline_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
1557 return(0);
1558 }
1559 if (name[len-1] != '\n')
1560 len++;
1561 temp = malloc(len);
1562 if (!temp) {
1563 getline_error = GETLINE_OUT_OF_MEM;
1564 return(0);
1565 }
1566 memcpy(temp, name, len-1);
1567 temp[len-1] = 0;
1568 return(temp);
1569}
1570
1068/*
1069 * no_op()
1070 * for those option functions where the archive format has nothing to do.
1071 * Return:
1072 * 0
1073 */
1074
1075#ifdef __STDC__

--- 54 unchanged lines hidden (view full) ---

1130#ifdef __STDC__
1131void
1132tar_usage(void)
1133#else
1134void
1135tar_usage()
1136#endif
1137{
1571/*
1572 * no_op()
1573 * for those option functions where the archive format has nothing to do.
1574 * Return:
1575 * 0
1576 */
1577
1578#ifdef __STDC__

--- 54 unchanged lines hidden (view full) ---

1633#ifdef __STDC__
1634void
1635tar_usage(void)
1636#else
1637void
1638tar_usage()
1639#endif
1640{
1138 (void)fputs("usage: tar -{txru}[cevfbmopwzBHLPXZ014578] [tapefile] ",
1641 (void)fputs("usage: tar [-]{crtux}[-befhjmopqsvwyzHLOPXZ014578] [blocksize] ",
1139 stderr);
1642 stderr);
1140 (void)fputs("[blocksize] file1 file2...\n", stderr);
1643 (void)fputs("[archive] [replstr] [-C directory] [-I file] [file ...]\n",
1644 stderr);
1141 exit(1);
1142}
1143
1645 exit(1);
1646}
1647
1144#ifdef notdef
1145/*
1146 * cpio_usage()
1147 * print the usage summary to the user
1148 */
1149
1150#ifdef __STDC__
1151void
1152cpio_usage(void)
1153#else
1154void
1155cpio_usage()
1156#endif
1157{
1648/*
1649 * cpio_usage()
1650 * print the usage summary to the user
1651 */
1652
1653#ifdef __STDC__
1654void
1655cpio_usage(void)
1656#else
1657void
1658cpio_usage()
1659#endif
1660{
1661 (void)fputs("usage: cpio -o [-aABcLvVzZ] [-C bytes] [-H format] [-O archive]\n", stderr);
1662 (void)fputs(" [-F archive] < name-list [> archive]\n", stderr);
1663 (void)fputs(" cpio -i [-bBcdfmnrsStuvVzZ6] [-C bytes] [-E file] [-H format]\n", stderr);
1664 (void)fputs(" [-I archive] [-F archive] [pattern...] [< archive]\n", stderr);
1665 (void)fputs(" cpio -p [-adlLmuvV] destination-directory < name-list\n", stderr);
1158 exit(1);
1159}
1666 exit(1);
1667}
1160#endif