pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / pax / options.c
blob66e963c044b9f6fb098449981ee942d2721c21e9
1 /*-
2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #define _POSIX_SOURCE 1
35 #define _MINIX 1
37 #if 0
38 #ifndef lint
39 static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 4/18/94";
40 #endif /* not lint */
41 #endif
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/mtio.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <unistd.h>
51 #include <stdlib.h>
52 #include <limits.h>
53 #include <minix/paths.h>
55 #include <sys/types.h>
56 #include <string.h>
57 #include <strings.h>
58 #include <unistd.h>
61 #include "pax.h"
62 #include "options.h"
63 #include "cpio.h"
64 #include "tar.h"
65 #include "extern.h"
68 * Routines which handle command line options
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 */
75 static int no_op(void);
76 static void printflg(unsigned int);
77 static int c_frmt(const void *, const void *);
78 static off_t str_offt(char *);
79 static char *getline(FILE *fp);
80 static void pax_options(int, char **);
81 static void pax_usage(void);
82 static void tar_options(int, char **);
83 static void tar_usage(void);
84 static void cpio_options(int, char **);
85 static void cpio_usage(void);
87 /* errors from getline */
88 #define GETLINE_FILE_CORRUPT 1
89 #define GETLINE_OUT_OF_MEM 2
90 static int getline_error;
93 #define GZIP_CMD "gzip" /* command to run as gzip */
94 #define COMPRESS_CMD "compress" /* command to run as compress */
95 #define BZIP2_CMD "bzip2" /* command to run as gzip */
98 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
99 * (see pax.h for description of each function)
101 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
102 * read, end_read, st_write, write, end_write, trail,
103 * rd_data, wr_data, options
106 FSUB fsub[] = {
107 /* 0: OLD BINARY CPIO */
108 {"bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
109 bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, cpio_trail,
110 NULL, rd_wrfile, wr_rdfile, bad_opt},
112 /* 1: OLD OCTAL CHARACTER CPIO */
113 {"cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
114 cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, cpio_trail,
115 NULL, rd_wrfile, wr_rdfile, bad_opt},
117 /* 2: SVR4 HEX CPIO */
118 {"sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
119 vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, cpio_trail,
120 NULL, rd_wrfile, wr_rdfile, bad_opt},
122 /* 3: SVR4 HEX CPIO WITH CRC */
123 {"sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
124 vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, cpio_trail,
125 NULL, rd_wrfile, wr_rdfile, bad_opt},
127 /* 4: OLD TAR */
128 {"tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
129 tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, NULL, tar_trail,
130 rd_wrfile, wr_rdfile, tar_opt},
132 /* 5: POSIX USTAR */
133 {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
134 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, NULL, tar_trail,
135 rd_wrfile, wr_rdfile, bad_opt},
137 #define F_OCPIO 0 /* format when called as cpio -6 */
138 #define F_ACPIO 1 /* format when called as cpio -c */
139 #define F_CPIO 3 /* format when called as cpio */
140 #define F_OTAR 4 /* format when called as tar -o */
141 #define F_TAR 5 /* format when called as tar */
142 #define DEFLT 5 /* default write format from list above */
145 * ford is the archive search order used by get_arc() to determine what kind
146 * of archive we are dealing with. This helps to properly id archive formats
147 * some formats may be subsets of others....
149 int ford[] = {5, 4, 3, 2, 1, 0, -1 };
152 * options()
153 * figure out if we are pax, tar or cpio. Call the appropriate options
154 * parser
157 void
158 options(int argc, char **argv)
162 * Are we acting like pax, tar or cpio (based on argv[0])
164 if ((argv0 = strrchr(argv[0], '/')) != NULL)
165 argv0++;
166 else
167 argv0 = argv[0];
169 if (strcmp(NM_TAR, argv0) == 0) {
170 tar_options(argc, argv);
171 return;
173 else if (strcmp(NM_CPIO, argv0) == 0) {
174 cpio_options(argc, argv);
175 return;
178 * assume pax as the default
180 argv0 = NM_PAX;
181 pax_options(argc, argv);
182 return;
186 * pax_options()
187 * look at the user specified flags. set globals as required and check if
188 * the user specified a legal set of flags. If not, complain and exit
191 static void
192 pax_options(int argc, char **argv)
194 int c;
195 size_t i;
196 unsigned int flg = 0;
197 unsigned int bflg = 0;
198 char *pt;
199 FSUB tmp;
202 * process option flags
204 while ((c=getopt(argc,argv,"ab:cdf:iklno:p:rs:tuvwx:zB:DE:G:HLPT:U:XYZ"))
205 != -1) {
206 switch (c) {
207 case 'a':
209 * append
211 flg |= AF;
212 break;
213 case 'b':
215 * specify blocksize
217 flg |= BF;
218 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
219 paxwarn(1, "Invalid block size %s", optarg);
220 pax_usage();
222 break;
223 case 'c':
225 * inverse match on patterns
227 cflag = 1;
228 flg |= CF;
229 break;
230 case 'd':
232 * match only dir on extract, not the subtree at dir
234 dflag = 1;
235 flg |= DF;
236 break;
237 case 'f':
239 * filename where the archive is stored
241 arcname = optarg;
242 flg |= FF;
243 break;
244 case 'i':
246 * interactive file rename
248 iflag = 1;
249 flg |= IF;
250 break;
251 case 'k':
253 * do not clobber files that exist
255 kflag = 1;
256 flg |= KF;
257 break;
258 case 'l':
260 * try to link src to dest with copy (-rw)
262 lflag = 1;
263 flg |= LF;
264 break;
265 case 'n':
267 * select first match for a pattern only
269 nflag = 1;
270 flg |= NF;
271 break;
272 case 'o':
274 * pass format specific options
276 flg |= OF;
277 if (opt_add(optarg) < 0)
278 pax_usage();
279 break;
280 case 'p':
282 * specify file characteristic options
284 for (pt = optarg; *pt != '\0'; ++pt) {
285 switch(*pt) {
286 case 'a':
288 * do not preserve access time
290 patime = 0;
291 break;
292 case 'e':
294 * preserve user id, group id, file
295 * mode, access/modification times
297 pids = 1;
298 pmode = 1;
299 patime = 1;
300 pmtime = 1;
301 break;
302 case 'm':
304 * do not preserve modification time
306 pmtime = 0;
307 break;
308 case 'o':
310 * preserve uid/gid
312 pids = 1;
313 break;
314 case 'p':
316 * preserver file mode bits
318 pmode = 1;
319 break;
320 default:
321 paxwarn(1, "Invalid -p string: %c", *pt);
322 pax_usage();
323 break;
326 flg |= PF;
327 break;
328 case 'r':
330 * read the archive
332 flg |= RF;
333 break;
334 case 's':
336 * file name substitution name pattern
338 if (rep_add(optarg) < 0) {
339 pax_usage();
340 break;
342 flg |= SF;
343 break;
344 case 't':
346 * preserve access time on file system nodes we read
348 tflag = 1;
349 flg |= TF;
350 break;
351 case 'u':
353 * ignore those older files
355 uflag = 1;
356 flg |= UF;
357 break;
358 case 'v':
360 * verbose operation mode
362 vflag = 1;
363 flg |= VF;
364 break;
365 case 'w':
367 * write an archive
369 flg |= WF;
370 break;
371 case 'x':
373 * specify an archive format on write
375 tmp.name = optarg;
376 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
377 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL) {
378 flg |= XF;
379 break;
381 paxwarn(1, "Unknown -x format: %s", optarg);
382 (void)fputs("pax: Known -x formats are:", stderr);
383 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
384 (void)fprintf(stderr, " %s", fsub[i].name);
385 (void)fputs("\n\n", stderr);
386 pax_usage();
387 break;
388 case 'z':
390 * use gzip. Non standard option.
392 gzip_program = GZIP_CMD;
393 break;
394 case 'B':
396 * non-standard option on number of bytes written on a
397 * single archive volume.
399 if ((wrlimit = str_offt(optarg)) <= 0) {
400 paxwarn(1, "Invalid write limit %s", optarg);
401 pax_usage();
403 if (wrlimit % BLKMULT) {
404 paxwarn(1, "Write limit is not a %d byte multiple",
405 BLKMULT);
406 pax_usage();
408 flg |= CBF;
409 break;
410 case 'D':
412 * On extraction check file inode change time before the
413 * modification of the file name. Non standard option.
415 Dflag = 1;
416 flg |= CDF;
417 break;
418 case 'E':
420 * non-standard limit on read faults
421 * 0 indicates stop after first error, values
422 * indicate a limit, "NONE" try forever
424 flg |= CEF;
425 if (strcmp(NONE, optarg) == 0)
426 maxflt = -1;
427 else if ((maxflt = atoi(optarg)) < 0) {
428 paxwarn(1, "Error count value must be positive");
429 pax_usage();
431 break;
432 case 'G':
434 * non-standard option for selecting files within an
435 * archive by group (gid or name)
437 if (grp_add(optarg) < 0) {
438 pax_usage();
439 break;
441 flg |= CGF;
442 break;
443 case 'H':
445 * follow command line symlinks only
447 Hflag = 1;
448 flg |= CHF;
449 break;
450 case 'L':
452 * follow symlinks
454 Lflag = 1;
455 flg |= CLF;
456 break;
457 case 'P':
459 * do NOT follow symlinks (default)
461 Lflag = 0;
462 flg |= CPF;
463 break;
464 case 'T':
466 * non-standard option for selecting files within an
467 * archive by modification time range (lower,upper)
469 if (trng_add(optarg) < 0) {
470 pax_usage();
471 break;
473 flg |= CTF;
474 break;
475 case 'U':
477 * non-standard option for selecting files within an
478 * archive by user (uid or name)
480 if (usr_add(optarg) < 0) {
481 pax_usage();
482 break;
484 flg |= CUF;
485 break;
486 case 'X':
488 * do not pass over mount points in the file system
490 Xflag = 1;
491 flg |= CXF;
492 break;
493 case 'Y':
495 * On extraction check file inode change time after the
496 * modification of the file name. Non standard option.
498 Yflag = 1;
499 flg |= CYF;
500 break;
501 case 'Z':
503 * On extraction check modification time after the
504 * modification of the file name. Non standard option.
506 Zflag = 1;
507 flg |= CZF;
508 break;
509 default:
510 pax_usage();
511 break;
516 * figure out the operation mode of pax read,write,extract,copy,append
517 * or list. check that we have not been given a bogus set of flags
518 * for the operation mode.
520 if (ISLIST(flg)) {
521 act = LIST;
522 listf = stdout;
523 bflg = flg & BDLIST;
524 } else if (ISEXTRACT(flg)) {
525 act = EXTRACT;
526 bflg = flg & BDEXTR;
527 } else if (ISARCHIVE(flg)) {
528 act = ARCHIVE;
529 bflg = flg & BDARCH;
530 } else if (ISAPPND(flg)) {
531 act = APPND;
532 bflg = flg & BDARCH;
533 } else if (ISCOPY(flg)) {
534 act = COPY;
535 bflg = flg & BDCOPY;
536 } else
537 pax_usage();
538 if (bflg) {
539 printflg(flg);
540 pax_usage();
544 * if we are writing (ARCHIVE) we use the default format if the user
545 * did not specify a format. when we write during an APPEND, we will
546 * adopt the format of the existing archive if none was supplied.
548 if (!(flg & XF) && (act == ARCHIVE))
549 frmt = &(fsub[DEFLT]);
552 * process the args as they are interpreted by the operation mode
554 switch (act) {
555 case LIST:
556 case EXTRACT:
557 for (; optind < argc; optind++)
558 if (pat_add(argv[optind], NULL) < 0)
559 pax_usage();
560 break;
561 case COPY:
562 if (optind >= argc) {
563 paxwarn(0, "Destination directory was not supplied");
564 pax_usage();
566 --argc;
567 dirptr = argv[argc];
568 /* FALLTHROUGH */
569 case ARCHIVE:
570 case APPND:
571 for (; optind < argc; optind++)
572 if (ftree_add(argv[optind], 0) < 0)
573 pax_usage();
575 * no read errors allowed on updates/append operation!
577 maxflt = 0;
578 break;
582 static void
583 tar_set_action(int op)
585 if (act != ERROR && act != op)
586 tar_usage();
587 act = op;
591 * tar_options()
592 * look at the user specified flags. set globals as required and check if
593 * the user specified a legal set of flags. If not, complain and exit
596 static void
597 tar_options(int argc, char **argv)
599 int c;
600 int fstdin = 0;
601 int Oflag = 0;
602 int nincfiles = 0;
603 int incfiles_max = 0;
604 struct incfile {
605 char *file;
606 char *dir;
608 struct incfile *incfiles = NULL;
611 * Set default values.
613 rmleadslash = 1;
616 * process option flags
618 while ((c = getoldopt(argc, argv,
619 "b:cef:hjmopqruts:vwxyzBC:HI:LOPXZ014578")) != -1) {
620 switch(c) {
621 case 'b':
623 * specify blocksize in 512-byte blocks
625 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
626 paxwarn(1, "Invalid block size %s", optarg);
627 tar_usage();
629 wrblksz *= 512; /* XXX - check for int oflow */
630 break;
631 case 'c':
633 * create an archive
635 tar_set_action(ARCHIVE);
636 break;
637 case 'e':
639 * stop after first error
641 maxflt = 0;
642 break;
643 case 'f':
645 * filename where the archive is stored
647 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
649 * treat a - as stdin
651 fstdin = 1;
652 arcname = NULL;
653 break;
655 fstdin = 0;
656 arcname = optarg;
657 break;
658 case 'h':
660 * follow symlinks
662 Lflag = 1;
663 break;
664 case 'j':
665 case 'y':
667 * use bzip2. Non standard option.
669 gzip_program = BZIP2_CMD;
670 break;
671 case 'm':
673 * do not preserve modification time
675 pmtime = 0;
676 break;
677 case 'o':
678 if (opt_add("write_opt=nodir") < 0)
679 tar_usage();
680 case 'O':
681 Oflag = 1;
682 break;
683 case 'p':
685 * preserve uid/gid and file mode, regardless of umask
687 pmode = 1;
688 pids = 1;
689 break;
690 case 'q':
692 * select first match for a pattern only
694 nflag = 1;
695 break;
696 case 'r':
697 case 'u':
699 * append to the archive
701 tar_set_action(APPND);
702 break;
703 case 's':
705 * file name substitution name pattern
707 if (rep_add(optarg) < 0) {
708 tar_usage();
709 break;
711 break;
712 case 't':
714 * list contents of the tape
716 tar_set_action(LIST);
717 break;
718 case 'v':
720 * verbose operation mode
722 vflag++;
723 break;
724 case 'w':
726 * interactive file rename
728 iflag = 1;
729 break;
730 case 'x':
732 * extract an archive, preserving mode,
733 * and mtime if possible.
735 tar_set_action(EXTRACT);
736 pmtime = 1;
737 break;
738 case 'z':
740 * use gzip. Non standard option.
742 gzip_program = GZIP_CMD;
743 break;
744 case 'B':
746 * Nothing to do here, this is pax default
748 break;
749 case 'C':
750 chdname = optarg;
751 break;
752 case 'H':
754 * follow command line symlinks only
756 Hflag = 1;
757 break;
758 case 'I':
759 if (++nincfiles > incfiles_max) {
760 incfiles_max = nincfiles + 3;
761 incfiles = realloc(incfiles,
762 sizeof(*incfiles) * incfiles_max);
763 if (incfiles == NULL) {
764 paxwarn(0, "Unable to allocate space "
765 "for option list");
766 exit(1);
769 incfiles[nincfiles - 1].file = optarg;
770 incfiles[nincfiles - 1].dir = chdname;
771 break;
772 case 'L':
774 * follow symlinks
776 Lflag = 1;
777 break;
778 case 'P':
780 * do not remove leading '/' from pathnames
782 rmleadslash = 0;
783 break;
784 case 'X':
786 * do not pass over mount points in the file system
788 Xflag = 1;
789 break;
790 case 'Z':
792 * use compress.
794 gzip_program = COMPRESS_CMD;
795 break;
796 case '0':
797 arcname = DEV_0;
798 break;
799 case '1':
800 arcname = DEV_1;
801 break;
802 case '4':
803 arcname = DEV_4;
804 break;
805 case '5':
806 arcname = DEV_5;
807 break;
808 case '7':
809 arcname = DEV_7;
810 break;
811 case '8':
812 arcname = DEV_8;
813 break;
814 default:
815 tar_usage();
816 break;
819 argc -= optind;
820 argv += optind;
822 /* Tar requires an action. */
823 if (act == ERROR)
824 tar_usage();
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;
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);
837 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
838 * (unless -o specified)
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 */
848 * process the args as they are interpreted by the operation mode
850 switch (act) {
851 case LIST:
852 case EXTRACT:
853 default:
855 int sawpat = 0;
856 char *file, *dir = NULL;
858 while (nincfiles || *argv != NULL) {
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.
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;
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();
888 while ((str = getline(fp)) != NULL) {
889 if (pat_add(str, dir) < 0)
890 tar_usage();
891 sawpat = 1;
893 if (strcmp(file, "-") != 0)
894 fclose(fp);
895 if (getline_error) {
896 paxwarn(1, "Problem with file '%s'", file);
897 tar_usage();
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;
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.
913 if (sawpat > 0)
914 chdname = NULL;
916 break;
917 case ARCHIVE:
918 case APPND:
919 if (chdname != NULL) { /* initial chdir() */
920 if (ftree_add(chdname, 1) < 0)
921 tar_usage();
924 while (nincfiles || *argv != NULL) {
925 char *file, *dir = NULL;
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.
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;
949 /* Set directory if needed */
950 if (dir) {
951 if (ftree_add(dir, 1) < 0)
952 tar_usage();
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();
961 while ((str = getline(fp)) != NULL) {
962 if (ftree_add(str, 0) < 0)
963 tar_usage();
965 if (strcmp(file, "-") != 0)
966 fclose(fp);
967 if (getline_error) {
968 paxwarn(1, "Problem with file '%s'",
969 file);
970 tar_usage();
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();
981 * no read errors allowed on updates/append operation!
983 maxflt = 0;
984 break;
986 if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
987 arcname = getenv("TAPE");
988 if ((arcname == NULL) || (*arcname == '\0'))
989 arcname = _PATH_DEFTAPE;
993 static int
994 mkpath(char *path)
996 struct stat sb;
997 char *slash;
998 int done = 0;
1000 slash = path;
1002 while (!done) {
1003 slash += strspn(slash, "/");
1004 slash += strcspn(slash, "/");
1006 done = (*slash == '\0');
1007 *slash = '\0';
1009 if (stat(path, &sb)) {
1010 if (errno != ENOENT || mkdir(path, 0777)) {
1011 paxwarn(1, "%s", path);
1012 return (-1);
1014 } else if (!S_ISDIR(sb.st_mode)) {
1015 syswarn(1, ENOTDIR, "%s", path);
1016 return (-1);
1019 if (!done)
1020 *slash = '/';
1023 return (0);
1026 * cpio_options()
1027 * look at the user specified flags. set globals as required and check if
1028 * the user specified a legal set of flags. If not, complain and exit
1031 static void
1032 cpio_options(int argc, char **argv)
1034 int c;
1035 size_t i;
1036 char *str;
1037 FSUB tmp;
1038 FILE *fp;
1040 kflag = 1;
1041 pids = 1;
1042 pmode = 1;
1043 pmtime = 0;
1044 arcname = NULL;
1045 dflag = 1;
1046 act = -1;
1047 nodirs = 1;
1048 while ((c=getopt(argc,argv,"abcdfiklmoprstuvzABC:E:F:H:I:LO:SZ6")) != -1)
1049 switch (c) {
1050 case 'a':
1052 * preserve access time on files read
1054 tflag = 1;
1055 break;
1056 case 'b':
1058 * swap bytes and half-words when reading data
1060 break;
1061 case 'c':
1063 * ASCII cpio header
1065 frmt = &(fsub[F_ACPIO]);
1066 break;
1067 case 'd':
1069 * create directories as needed
1071 nodirs = 0;
1072 break;
1073 case 'f':
1075 * invert meaning of pattern list
1077 cflag = 1;
1078 break;
1079 case 'i':
1081 * restore an archive
1083 act = EXTRACT;
1084 break;
1085 case 'k':
1086 break;
1087 case 'l':
1089 * use links instead of copies when possible
1091 lflag = 1;
1092 break;
1093 case 'm':
1095 * preserve modification time
1097 pmtime = 1;
1098 break;
1099 case 'o':
1101 * create an archive
1103 act = ARCHIVE;
1104 frmt = &(fsub[F_CPIO]);
1105 break;
1106 case 'p':
1108 * copy-pass mode
1110 act = COPY;
1111 break;
1112 case 'r':
1114 * interactively rename files
1116 iflag = 1;
1117 break;
1118 case 's':
1120 * swap bytes after reading data
1122 break;
1123 case 't':
1125 * list contents of archive
1127 act = LIST;
1128 listf = stdout;
1129 break;
1130 case 'u':
1132 * replace newer files
1134 kflag = 0;
1135 break;
1136 case 'v':
1138 * verbose operation mode
1140 vflag = 1;
1141 break;
1142 case 'z':
1144 * use gzip. Non standard option.
1146 gzip_program = GZIP_CMD;
1147 break;
1148 case 'A':
1150 * append mode
1152 act = APPND;
1153 break;
1154 case 'B':
1156 * Use 5120 byte block size
1158 wrblksz = 5120;
1159 break;
1160 case 'C':
1162 * set block size in bytes
1164 wrblksz = atoi(optarg);
1165 break;
1166 case 'E':
1168 * file with patterns to extract or list
1170 if ((fp = fopen(optarg, "r")) == NULL) {
1171 paxwarn(1, "Unable to open file '%s' for read", optarg);
1172 cpio_usage();
1174 while ((str = getline(fp)) != NULL) {
1175 pat_add(str, NULL);
1177 fclose(fp);
1178 if (getline_error) {
1179 paxwarn(1, "Problem with file '%s'", optarg);
1180 cpio_usage();
1182 break;
1183 case 'F':
1184 case 'I':
1185 case 'O':
1187 * filename where the archive is stored
1189 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1191 * treat a - as stdin
1193 arcname = NULL;
1194 break;
1196 arcname = optarg;
1197 break;
1198 case 'H':
1200 * specify an archive format on write
1202 tmp.name = optarg;
1203 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1204 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL)
1205 break;
1206 paxwarn(1, "Unknown -H format: %s", optarg);
1207 (void)fputs("cpio: Known -H formats are:", stderr);
1208 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1209 (void)fprintf(stderr, " %s", fsub[i].name);
1210 (void)fputs("\n\n", stderr);
1211 cpio_usage();
1212 break;
1213 case 'L':
1215 * follow symbolic links
1217 Lflag = 1;
1218 break;
1219 case 'S':
1221 * swap halfwords after reading data
1223 break;
1224 case 'Z':
1226 * use compress. Non standard option.
1228 gzip_program = COMPRESS_CMD;
1229 break;
1230 case '6':
1232 * process Version 6 cpio format
1234 frmt = &(fsub[F_OCPIO]);
1235 break;
1236 case '?':
1237 default:
1238 cpio_usage();
1239 break;
1241 argc -= optind;
1242 argv += optind;
1245 * process the args as they are interpreted by the operation mode
1247 switch (act) {
1248 case LIST:
1249 case EXTRACT:
1250 while (*argv != NULL)
1251 if (pat_add(*argv++, NULL) < 0)
1252 cpio_usage();
1253 break;
1254 case COPY:
1255 if (*argv == NULL) {
1256 paxwarn(0, "Destination directory was not supplied");
1257 cpio_usage();
1259 dirptr = *argv;
1260 if (mkpath(dirptr) < 0)
1261 cpio_usage();
1262 --argc;
1263 ++argv;
1264 /* FALLTHROUGH */
1265 case ARCHIVE:
1266 case APPND:
1267 if (*argv != NULL)
1268 cpio_usage();
1270 * no read errors allowed on updates/append operation!
1272 maxflt = 0;
1273 while ((str = getline(stdin)) != NULL) {
1274 ftree_add(str, 0);
1276 if (getline_error) {
1277 paxwarn(1, "Problem while reading stdin");
1278 cpio_usage();
1280 break;
1281 default:
1282 cpio_usage();
1283 break;
1288 * printflg()
1289 * print out those invalid flag sets found to the user
1292 static void
1293 printflg(unsigned int flg)
1295 int nxt;
1296 int pos = 0;
1298 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1299 while ((nxt = ffs(flg)) != 0) {
1300 flg = flg >> nxt;
1301 pos += nxt;
1302 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1304 (void)putc('\n', stderr);
1308 * c_frmt()
1309 * comparison routine used by bsearch to find the format specified
1310 * by the user
1313 static int
1314 c_frmt(const void *a, const void *b)
1316 return(strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name));
1320 * opt_next()
1321 * called by format specific options routines to get each format specific
1322 * flag and value specified with -o
1323 * Return:
1324 * pointer to next OPLIST entry or NULL (end of list).
1327 OPLIST *
1328 opt_next(void)
1330 OPLIST *opt;
1332 if ((opt = ophead) != NULL)
1333 ophead = ophead->fow;
1334 return(opt);
1338 * bad_opt()
1339 * generic routine used to complain about a format specific options
1340 * when the format does not support options.
1344 bad_opt(void)
1346 OPLIST *opt;
1348 if (ophead == NULL)
1349 return(0);
1351 * print all we were given
1353 paxwarn(1,"These format options are not supported");
1354 while ((opt = opt_next()) != NULL)
1355 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1356 pax_usage();
1357 return(0);
1361 * opt_add()
1362 * breaks the value supplied to -o into an option name and value. Options
1363 * are given to -o in the form -o name-value,name=value
1364 * multiple -o may be specified.
1365 * Return:
1366 * 0 if format in name=value format, -1 if -o is passed junk.
1370 opt_add(const char *str)
1372 OPLIST *opt;
1373 char *frpt;
1374 char *pt;
1375 char *endpt;
1376 char *lstr;
1378 if ((str == NULL) || (*str == '\0')) {
1379 paxwarn(0, "Invalid option name");
1380 return(-1);
1382 if ((lstr = strdup(str)) == NULL) {
1383 paxwarn(0, "Unable to allocate space for option list");
1384 return(-1);
1386 frpt = endpt = lstr;
1389 * break into name and values pieces and stuff each one into a
1390 * OPLIST structure. When we know the format, the format specific
1391 * option function will go through this list
1393 while ((frpt != NULL) && (*frpt != '\0')) {
1394 if ((endpt = strchr(frpt, ',')) != NULL)
1395 *endpt = '\0';
1396 if ((pt = strchr(frpt, '=')) == NULL) {
1397 paxwarn(0, "Invalid options format");
1398 free(lstr);
1399 return(-1);
1401 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1402 paxwarn(0, "Unable to allocate space for option list");
1403 free(lstr);
1404 return(-1);
1406 *pt++ = '\0';
1407 opt->name = frpt;
1408 opt->value = pt;
1409 opt->fow = NULL;
1410 if (endpt != NULL)
1411 frpt = endpt + 1;
1412 else
1413 frpt = NULL;
1414 if (ophead == NULL) {
1415 optail = ophead = opt;
1416 continue;
1418 optail->fow = opt;
1419 optail = opt;
1421 return(0);
1425 * str_offt()
1426 * Convert an expression of the following forms to an off_t > 0.
1427 * 1) A positive decimal number.
1428 * 2) A positive decimal number followed by a b (mult by 512).
1429 * 3) A positive decimal number followed by a k (mult by 1024).
1430 * 4) A positive decimal number followed by a m (mult by 512).
1431 * 5) A positive decimal number followed by a w (mult by sizeof int)
1432 * 6) Two or more positive decimal numbers (with/without k,b or w).
1433 * separated by x (also * for backwards compatibility), specifying
1434 * the product of the indicated values.
1435 * Return:
1436 * 0 for an error, a positive value o.w.
1439 static off_t
1440 str_offt(char *val)
1442 char *expr;
1443 off_t num, t;
1445 # ifdef NET2_STAT
1446 num = strtol(val, &expr, 0);
1447 if ((num == LONG_MAX) || (num <= 0) || (expr == val))
1448 # else
1449 num = strtoq(val, &expr, 0);
1450 if ((num == QUAD_MAX) || (num <= 0) || (expr == val))
1451 # endif
1452 return(0);
1454 switch(*expr) {
1455 case 'b':
1456 t = num;
1457 num *= 512;
1458 if (t > num)
1459 return(0);
1460 ++expr;
1461 break;
1462 case 'k':
1463 t = num;
1464 num *= 1024;
1465 if (t > num)
1466 return(0);
1467 ++expr;
1468 break;
1469 case 'm':
1470 t = num;
1471 num *= 1048576;
1472 if (t > num)
1473 return(0);
1474 ++expr;
1475 break;
1476 case 'w':
1477 t = num;
1478 num *= sizeof(int);
1479 if (t > num)
1480 return(0);
1481 ++expr;
1482 break;
1485 switch(*expr) {
1486 case '\0':
1487 break;
1488 case '*':
1489 case 'x':
1490 t = num;
1491 num *= str_offt(expr + 1);
1492 if (t > num)
1493 return(0);
1494 break;
1495 default:
1496 return(0);
1498 return(num);
1501 char *fgetln(FILE *f, size_t *);
1503 static char *
1504 getline(FILE *f)
1506 char *name, *temp;
1507 size_t len;
1509 name = fgetln(f, &len);
1510 if (!name) {
1511 getline_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
1512 return(0);
1514 if (name[len-1] != '\n')
1515 len++;
1516 temp = malloc(len);
1517 if (!temp) {
1518 getline_error = GETLINE_OUT_OF_MEM;
1519 return(0);
1521 memcpy(temp, name, len-1);
1522 temp[len-1] = 0;
1523 return(temp);
1527 * no_op()
1528 * for those option functions where the archive format has nothing to do.
1529 * Return:
1533 static int
1534 no_op(void)
1536 return(0);
1540 * pax_usage()
1541 * print the usage summary to the user
1544 static void
1545 pax_usage(void)
1547 (void)fputs("usage: pax [-cdnvz] [-E limit] [-f archive] ", stderr);
1548 (void)fputs("[-s replstr] ... [-U user] ...", stderr);
1549 (void)fputs("\n [-G group] ... ", stderr);
1550 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1551 (void)fputs("[pattern ...]\n", stderr);
1552 (void)fputs(" pax -r [-cdiknuvzDYZ] [-E limit] ", stderr);
1553 (void)fputs("[-f archive] [-o options] ... \n", stderr);
1554 (void)fputs(" [-p string] ... [-s replstr] ... ", stderr);
1555 (void)fputs("[-U user] ... [-G group] ...\n ", stderr);
1556 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1557 (void)fputs(" [pattern ...]\n", stderr);
1558 (void)fputs(" pax -w [-dituvzHLPX] [-b blocksize] ", stderr);
1559 (void)fputs("[ [-a] [-f archive] ] [-x format] \n", stderr);
1560 (void)fputs(" [-B bytes] [-s replstr] ... ", stderr);
1561 (void)fputs("[-o options] ... [-U user] ...", stderr);
1562 (void)fputs("\n [-G group] ... ", stderr);
1563 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1564 (void)fputs("[file ...]\n", stderr);
1565 (void)fputs(" pax -r -w [-diklntuvDHLPXYZ] ", stderr);
1566 (void)fputs("[-p string] ... [-s replstr] ...", stderr);
1567 (void)fputs("\n [-U user] ... [-G group] ... ", stderr);
1568 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1569 (void)fputs("\n [file ...] directory\n", stderr);
1570 exit(1);
1574 * tar_usage()
1575 * print the usage summary to the user
1578 static void
1579 tar_usage(void)
1581 (void)fputs("usage: tar [-]{crtux}[-befhjmopqsvwyzHLOPXZ014578] [blocksize] ",
1582 stderr);
1583 (void)fputs("[archive] [replstr] [-C directory] [-I file] [file ...]\n",
1584 stderr);
1585 exit(1);
1589 * cpio_usage()
1590 * print the usage summary to the user
1593 static void
1594 cpio_usage(void)
1596 (void)fputs("usage: cpio -o [-aABcLvVzZ] [-C bytes] [-H format] [-O archive]\n", stderr);
1597 (void)fputs(" [-F archive] < name-list [> archive]\n", stderr);
1598 (void)fputs(" cpio -i [-bBcdfmnrsStuvVzZ6] [-C bytes] [-E file] [-H format]\n", stderr);
1599 (void)fputs(" [-I archive] [-F archive] [pattern...] [< archive]\n", stderr);
1600 (void)fputs(" cpio -p [-adlLmuvV] destination-directory < name-list\n", stderr);
1601 exit(1);