minor fixes for safecopy & safemap tests
[minix.git] / commands / pax / options.c
blob8f5547abcb692d9c2886a9cf72b5ed25e66aec1a
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 <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 #ifdef __NBSD_LIBC
72 #define getline pax_getline
73 #endif
75 static char flgch[] = FLGCH; /* list of all possible flags */
76 static OPLIST *ophead = NULL; /* head for format specific options -x */
77 static OPLIST *optail = NULL; /* option tail */
79 static int no_op(void);
80 static void printflg(unsigned int);
81 static int c_frmt(const void *, const void *);
82 static off_t str_offt(char *);
83 static char *getline(FILE *fp);
84 static void pax_options(int, char **);
85 static void pax_usage(void);
86 static void tar_options(int, char **);
87 static void tar_usage(void);
88 static void cpio_options(int, char **);
89 static void cpio_usage(void);
91 /* errors from getline */
92 #define GETLINE_FILE_CORRUPT 1
93 #define GETLINE_OUT_OF_MEM 2
94 static int getline_error;
97 #define GZIP_CMD "gzip" /* command to run as gzip */
98 #define COMPRESS_CMD "compress" /* command to run as compress */
99 #define BZIP2_CMD "bzip2" /* command to run as gzip */
102 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
103 * (see pax.h for description of each function)
105 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
106 * read, end_read, st_write, write, end_write, trail,
107 * rd_data, wr_data, options
110 FSUB fsub[] = {
111 /* 0: OLD BINARY CPIO */
112 {"bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
113 bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, cpio_trail,
114 NULL, rd_wrfile, wr_rdfile, bad_opt},
116 /* 1: OLD OCTAL CHARACTER CPIO */
117 {"cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
118 cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, cpio_trail,
119 NULL, rd_wrfile, wr_rdfile, bad_opt},
121 /* 2: SVR4 HEX CPIO */
122 {"sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
123 vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, cpio_trail,
124 NULL, rd_wrfile, wr_rdfile, bad_opt},
126 /* 3: SVR4 HEX CPIO WITH CRC */
127 {"sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
128 vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, cpio_trail,
129 NULL, rd_wrfile, wr_rdfile, bad_opt},
131 /* 4: OLD TAR */
132 {"tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
133 tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, NULL, tar_trail,
134 rd_wrfile, wr_rdfile, tar_opt},
136 /* 5: POSIX USTAR */
137 {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
138 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, NULL, tar_trail,
139 rd_wrfile, wr_rdfile, bad_opt},
141 #define F_OCPIO 0 /* format when called as cpio -6 */
142 #define F_ACPIO 1 /* format when called as cpio -c */
143 #define F_CPIO 3 /* format when called as cpio */
144 #define F_OTAR 4 /* format when called as tar -o */
145 #define F_TAR 5 /* format when called as tar */
146 #define DEFLT 5 /* default write format from list above */
149 * ford is the archive search order used by get_arc() to determine what kind
150 * of archive we are dealing with. This helps to properly id archive formats
151 * some formats may be subsets of others....
153 int ford[] = {5, 4, 3, 2, 1, 0, -1 };
156 * options()
157 * figure out if we are pax, tar or cpio. Call the appropriate options
158 * parser
161 void
162 options(int argc, char **argv)
166 * Are we acting like pax, tar or cpio (based on argv[0])
168 if ((argv0 = strrchr(argv[0], '/')) != NULL)
169 argv0++;
170 else
171 argv0 = argv[0];
173 if (strcmp(NM_TAR, argv0) == 0) {
174 tar_options(argc, argv);
175 return;
177 else if (strcmp(NM_CPIO, argv0) == 0) {
178 cpio_options(argc, argv);
179 return;
182 * assume pax as the default
184 argv0 = NM_PAX;
185 pax_options(argc, argv);
186 return;
190 * pax_options()
191 * look at the user specified flags. set globals as required and check if
192 * the user specified a legal set of flags. If not, complain and exit
195 static void
196 pax_options(int argc, char **argv)
198 int c;
199 size_t i;
200 unsigned int flg = 0;
201 unsigned int bflg = 0;
202 char *pt;
203 FSUB tmp;
206 * process option flags
208 while ((c=getopt(argc,argv,"ab:cdf:iklno:p:rs:tuvwx:zB:DE:G:HLPT:U:XYZ"))
209 != -1) {
210 switch (c) {
211 case 'a':
213 * append
215 flg |= AF;
216 break;
217 case 'b':
219 * specify blocksize
221 flg |= BF;
222 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
223 paxwarn(1, "Invalid block size %s", optarg);
224 pax_usage();
226 break;
227 case 'c':
229 * inverse match on patterns
231 cflag = 1;
232 flg |= CF;
233 break;
234 case 'd':
236 * match only dir on extract, not the subtree at dir
238 dflag = 1;
239 flg |= DF;
240 break;
241 case 'f':
243 * filename where the archive is stored
245 arcname = optarg;
246 flg |= FF;
247 break;
248 case 'i':
250 * interactive file rename
252 iflag = 1;
253 flg |= IF;
254 break;
255 case 'k':
257 * do not clobber files that exist
259 kflag = 1;
260 flg |= KF;
261 break;
262 case 'l':
264 * try to link src to dest with copy (-rw)
266 lflag = 1;
267 flg |= LF;
268 break;
269 case 'n':
271 * select first match for a pattern only
273 nflag = 1;
274 flg |= NF;
275 break;
276 case 'o':
278 * pass format specific options
280 flg |= OF;
281 if (opt_add(optarg) < 0)
282 pax_usage();
283 break;
284 case 'p':
286 * specify file characteristic options
288 for (pt = optarg; *pt != '\0'; ++pt) {
289 switch(*pt) {
290 case 'a':
292 * do not preserve access time
294 patime = 0;
295 break;
296 case 'e':
298 * preserve user id, group id, file
299 * mode, access/modification times
301 pids = 1;
302 pmode = 1;
303 patime = 1;
304 pmtime = 1;
305 break;
306 case 'm':
308 * do not preserve modification time
310 pmtime = 0;
311 break;
312 case 'o':
314 * preserve uid/gid
316 pids = 1;
317 break;
318 case 'p':
320 * preserver file mode bits
322 pmode = 1;
323 break;
324 default:
325 paxwarn(1, "Invalid -p string: %c", *pt);
326 pax_usage();
327 break;
330 flg |= PF;
331 break;
332 case 'r':
334 * read the archive
336 flg |= RF;
337 break;
338 case 's':
340 * file name substitution name pattern
342 if (rep_add(optarg) < 0) {
343 pax_usage();
344 break;
346 flg |= SF;
347 break;
348 case 't':
350 * preserve access time on file system nodes we read
352 tflag = 1;
353 flg |= TF;
354 break;
355 case 'u':
357 * ignore those older files
359 uflag = 1;
360 flg |= UF;
361 break;
362 case 'v':
364 * verbose operation mode
366 vflag = 1;
367 flg |= VF;
368 break;
369 case 'w':
371 * write an archive
373 flg |= WF;
374 break;
375 case 'x':
377 * specify an archive format on write
379 tmp.name = optarg;
380 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
381 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL) {
382 flg |= XF;
383 break;
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);
390 pax_usage();
391 break;
392 case 'z':
394 * use gzip. Non standard option.
396 gzip_program = GZIP_CMD;
397 break;
398 case 'B':
400 * non-standard option on number of bytes written on a
401 * single archive volume.
403 if ((wrlimit = str_offt(optarg)) <= 0) {
404 paxwarn(1, "Invalid write limit %s", optarg);
405 pax_usage();
407 if (wrlimit % BLKMULT) {
408 paxwarn(1, "Write limit is not a %d byte multiple",
409 BLKMULT);
410 pax_usage();
412 flg |= CBF;
413 break;
414 case 'D':
416 * On extraction check file inode change time before the
417 * modification of the file name. Non standard option.
419 Dflag = 1;
420 flg |= CDF;
421 break;
422 case 'E':
424 * non-standard limit on read faults
425 * 0 indicates stop after first error, values
426 * indicate a limit, "NONE" try forever
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();
435 break;
436 case 'G':
438 * non-standard option for selecting files within an
439 * archive by group (gid or name)
441 if (grp_add(optarg) < 0) {
442 pax_usage();
443 break;
445 flg |= CGF;
446 break;
447 case 'H':
449 * follow command line symlinks only
451 Hflag = 1;
452 flg |= CHF;
453 break;
454 case 'L':
456 * follow symlinks
458 Lflag = 1;
459 flg |= CLF;
460 break;
461 case 'P':
463 * do NOT follow symlinks (default)
465 Lflag = 0;
466 flg |= CPF;
467 break;
468 case 'T':
470 * non-standard option for selecting files within an
471 * archive by modification time range (lower,upper)
473 if (trng_add(optarg) < 0) {
474 pax_usage();
475 break;
477 flg |= CTF;
478 break;
479 case 'U':
481 * non-standard option for selecting files within an
482 * archive by user (uid or name)
484 if (usr_add(optarg) < 0) {
485 pax_usage();
486 break;
488 flg |= CUF;
489 break;
490 case 'X':
492 * do not pass over mount points in the file system
494 Xflag = 1;
495 flg |= CXF;
496 break;
497 case 'Y':
499 * On extraction check file inode change time after the
500 * modification of the file name. Non standard option.
502 Yflag = 1;
503 flg |= CYF;
504 break;
505 case 'Z':
507 * On extraction check modification time after the
508 * modification of the file name. Non standard option.
510 Zflag = 1;
511 flg |= CZF;
512 break;
513 default:
514 pax_usage();
515 break;
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.
524 if (ISLIST(flg)) {
525 act = LIST;
526 listf = stdout;
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)) {
535 act = APPND;
536 bflg = flg & BDARCH;
537 } else if (ISCOPY(flg)) {
538 act = COPY;
539 bflg = flg & BDCOPY;
540 } else
541 pax_usage();
542 if (bflg) {
543 printflg(flg);
544 pax_usage();
548 * if we are writing (ARCHIVE) we use the default format if the user
549 * did not specify a format. when we write during an APPEND, we will
550 * adopt the format of the existing archive if none was supplied.
552 if (!(flg & XF) && (act == ARCHIVE))
553 frmt = &(fsub[DEFLT]);
556 * process the args as they are interpreted by the operation mode
558 switch (act) {
559 case LIST:
560 case EXTRACT:
561 for (; optind < argc; optind++)
562 if (pat_add(argv[optind], NULL) < 0)
563 pax_usage();
564 break;
565 case COPY:
566 if (optind >= argc) {
567 paxwarn(0, "Destination directory was not supplied");
568 pax_usage();
570 --argc;
571 dirptr = argv[argc];
572 /* FALLTHROUGH */
573 case ARCHIVE:
574 case APPND:
575 for (; optind < argc; optind++)
576 if (ftree_add(argv[optind], 0) < 0)
577 pax_usage();
579 * no read errors allowed on updates/append operation!
581 maxflt = 0;
582 break;
586 static void
587 tar_set_action(int op)
589 if (act != ERROR && act != op)
590 tar_usage();
591 act = op;
595 * tar_options()
596 * look at the user specified flags. set globals as required and check if
597 * the user specified a legal set of flags. If not, complain and exit
600 static void
601 tar_options(int argc, char **argv)
603 int c;
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;
612 struct incfile *incfiles = NULL;
615 * Set default values.
617 rmleadslash = 1;
620 * process option flags
622 while ((c = getoldopt(argc, argv,
623 "b:cef:hjmopqruts:vwxyzBC:HI:LOPXZ014578")) != -1) {
624 switch(c) {
625 case 'b':
627 * specify blocksize in 512-byte blocks
629 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
630 paxwarn(1, "Invalid block size %s", optarg);
631 tar_usage();
633 wrblksz *= 512; /* XXX - check for int oflow */
634 break;
635 case 'c':
637 * create an archive
639 tar_set_action(ARCHIVE);
640 break;
641 case 'e':
643 * stop after first error
645 maxflt = 0;
646 break;
647 case 'f':
649 * filename where the archive is stored
651 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
653 * treat a - as stdin
655 fstdin = 1;
656 arcname = NULL;
657 break;
659 fstdin = 0;
660 arcname = optarg;
661 break;
662 case 'h':
664 * follow symlinks
666 Lflag = 1;
667 break;
668 case 'j':
669 case 'y':
671 * use bzip2. Non standard option.
673 gzip_program = BZIP2_CMD;
674 break;
675 case 'm':
677 * do not preserve modification time
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;
686 break;
687 case 'p':
689 * preserve uid/gid and file mode, regardless of umask
691 pmode = 1;
692 pids = 1;
693 break;
694 case 'q':
696 * select first match for a pattern only
698 nflag = 1;
699 break;
700 case 'r':
701 case 'u':
703 * append to the archive
705 tar_set_action(APPND);
706 break;
707 case 's':
709 * file name substitution name pattern
711 if (rep_add(optarg) < 0) {
712 tar_usage();
713 break;
715 break;
716 case 't':
718 * list contents of the tape
720 tar_set_action(LIST);
721 break;
722 case 'v':
724 * verbose operation mode
726 vflag++;
727 break;
728 case 'w':
730 * interactive file rename
732 iflag = 1;
733 break;
734 case 'x':
736 * extract an archive, preserving mode,
737 * and mtime if possible.
739 tar_set_action(EXTRACT);
740 pmtime = 1;
741 break;
742 case 'z':
744 * use gzip. Non standard option.
746 gzip_program = GZIP_CMD;
747 break;
748 case 'B':
750 * Nothing to do here, this is pax default
752 break;
753 case 'C':
754 chdname = optarg;
755 break;
756 case 'H':
758 * follow command line symlinks only
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);
773 incfiles[nincfiles - 1].file = optarg;
774 incfiles[nincfiles - 1].dir = chdname;
775 break;
776 case 'L':
778 * follow symlinks
780 Lflag = 1;
781 break;
782 case 'P':
784 * do not remove leading '/' from pathnames
786 rmleadslash = 0;
787 break;
788 case 'X':
790 * do not pass over mount points in the file system
792 Xflag = 1;
793 break;
794 case 'Z':
796 * use compress.
798 gzip_program = COMPRESS_CMD;
799 break;
800 case '0':
801 arcname = DEV_0;
802 break;
803 case '1':
804 arcname = DEV_1;
805 break;
806 case '4':
807 arcname = DEV_4;
808 break;
809 case '5':
810 arcname = DEV_5;
811 break;
812 case '7':
813 arcname = DEV_7;
814 break;
815 case '8':
816 arcname = DEV_8;
817 break;
818 default:
819 tar_usage();
820 break;
823 argc -= optind;
824 argv += optind;
826 /* Tar requires an action. */
827 if (act == ERROR)
828 tar_usage();
830 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
831 if (fstdin == 1 && act == ARCHIVE)
832 listf = stderr;
833 else
834 listf = stdout;
836 /* Traditional tar behaviour (pax wants to read file list from stdin) */
837 if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
838 exit(0);
841 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
842 * (unless -o specified)
844 if (act == ARCHIVE || act == APPND)
845 frmt = &(fsub[Oflag ? F_OTAR : F_TAR]);
846 else if (Oflag) {
847 paxwarn(1, "The -O/-o options are only valid when writing an archive");
848 tar_usage(); /* only valid when writing */
852 * process the args as they are interpreted by the operation mode
854 switch (act) {
855 case LIST:
856 case EXTRACT:
857 default:
859 int sawpat = 0;
860 char *file, *dir = NULL;
862 while (nincfiles || *argv != NULL) {
864 * If we queued up any include files,
865 * pull them in now. Otherwise, check
866 * for -I and -C positional flags.
867 * Anything else must be a file to
868 * extract.
870 if (nincfiles) {
871 file = incfiles->file;
872 dir = incfiles->dir;
873 incfiles++;
874 nincfiles--;
875 } else if (strcmp(*argv, "-I") == 0) {
876 if (*++argv == NULL)
877 break;
878 file = *argv++;
879 dir = chdname;
880 } else
881 file = NULL;
882 if (file != NULL) {
883 FILE *fp;
884 char *str;
886 if (strcmp(file, "-") == 0)
887 fp = stdin;
888 else if ((fp = fopen(file, "r")) == NULL) {
889 paxwarn(1, "Unable to open file '%s' for read", file);
890 tar_usage();
892 while ((str = getline(fp)) != NULL) {
893 if (pat_add(str, dir) < 0)
894 tar_usage();
895 sawpat = 1;
897 if (strcmp(file, "-") != 0)
898 fclose(fp);
899 if (getline_error) {
900 paxwarn(1, "Problem with file '%s'", file);
901 tar_usage();
903 } else if (strcmp(*argv, "-C") == 0) {
904 if (*++argv == NULL)
905 break;
906 chdname = *argv++;
907 } else if (pat_add(*argv++, chdname) < 0)
908 tar_usage();
909 else
910 sawpat = 1;
913 * if patterns were added, we are doing chdir()
914 * on a file-by-file basis, else, just one
915 * global chdir (if any) after opening input.
917 if (sawpat > 0)
918 chdname = NULL;
920 break;
921 case ARCHIVE:
922 case APPND:
923 if (chdname != NULL) { /* initial chdir() */
924 if (ftree_add(chdname, 1) < 0)
925 tar_usage();
928 while (nincfiles || *argv != NULL) {
929 char *file, *dir = NULL;
932 * If we queued up any include files, pull them in
933 * now. Otherwise, check for -I and -C positional
934 * flags. Anything else must be a file to include
935 * in the archive.
937 if (nincfiles) {
938 file = incfiles->file;
939 dir = incfiles->dir;
940 incfiles++;
941 nincfiles--;
942 } else if (strcmp(*argv, "-I") == 0) {
943 if (*++argv == NULL)
944 break;
945 file = *argv++;
946 dir = NULL;
947 } else
948 file = NULL;
949 if (file != NULL) {
950 FILE *fp;
951 char *str;
953 /* Set directory if needed */
954 if (dir) {
955 if (ftree_add(dir, 1) < 0)
956 tar_usage();
959 if (strcmp(file, "-") == 0)
960 fp = stdin;
961 else if ((fp = fopen(file, "r")) == NULL) {
962 paxwarn(1, "Unable to open file '%s' for read", file);
963 tar_usage();
965 while ((str = getline(fp)) != NULL) {
966 if (ftree_add(str, 0) < 0)
967 tar_usage();
969 if (strcmp(file, "-") != 0)
970 fclose(fp);
971 if (getline_error) {
972 paxwarn(1, "Problem with file '%s'",
973 file);
974 tar_usage();
976 } else if (strcmp(*argv, "-C") == 0) {
977 if (*++argv == NULL)
978 break;
979 if (ftree_add(*argv++, 1) < 0)
980 tar_usage();
981 } else if (ftree_add(*argv++, 0) < 0)
982 tar_usage();
985 * no read errors allowed on updates/append operation!
987 maxflt = 0;
988 break;
990 if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
991 arcname = getenv("TAPE");
992 if ((arcname == NULL) || (*arcname == '\0'))
993 arcname = _PATH_DEFTAPE;
997 static int
998 mkpath(char *path)
1000 struct stat sb;
1001 char *slash;
1002 int done = 0;
1004 slash = path;
1006 while (!done) {
1007 slash += strspn(slash, "/");
1008 slash += strcspn(slash, "/");
1010 done = (*slash == '\0');
1011 *slash = '\0';
1013 if (stat(path, &sb)) {
1014 if (errno != ENOENT || mkdir(path, 0777)) {
1015 paxwarn(1, "%s", path);
1016 return (-1);
1018 } else if (!S_ISDIR(sb.st_mode)) {
1019 syswarn(1, ENOTDIR, "%s", path);
1020 return (-1);
1023 if (!done)
1024 *slash = '/';
1027 return (0);
1030 * cpio_options()
1031 * look at the user specified flags. set globals as required and check if
1032 * the user specified a legal set of flags. If not, complain and exit
1035 static void
1036 cpio_options(int argc, char **argv)
1038 int c;
1039 size_t i;
1040 char *str;
1041 FSUB tmp;
1042 FILE *fp;
1044 kflag = 1;
1045 pids = 1;
1046 pmode = 1;
1047 pmtime = 0;
1048 arcname = NULL;
1049 dflag = 1;
1050 act = -1;
1051 nodirs = 1;
1052 while ((c=getopt(argc,argv,"abcdfiklmoprstuvzABC:E:F:H:I:LO:SZ6")) != -1)
1053 switch (c) {
1054 case 'a':
1056 * preserve access time on files read
1058 tflag = 1;
1059 break;
1060 case 'b':
1062 * swap bytes and half-words when reading data
1064 break;
1065 case 'c':
1067 * ASCII cpio header
1069 frmt = &(fsub[F_ACPIO]);
1070 break;
1071 case 'd':
1073 * create directories as needed
1075 nodirs = 0;
1076 break;
1077 case 'f':
1079 * invert meaning of pattern list
1081 cflag = 1;
1082 break;
1083 case 'i':
1085 * restore an archive
1087 act = EXTRACT;
1088 break;
1089 case 'k':
1090 break;
1091 case 'l':
1093 * use links instead of copies when possible
1095 lflag = 1;
1096 break;
1097 case 'm':
1099 * preserve modification time
1101 pmtime = 1;
1102 break;
1103 case 'o':
1105 * create an archive
1107 act = ARCHIVE;
1108 frmt = &(fsub[F_CPIO]);
1109 break;
1110 case 'p':
1112 * copy-pass mode
1114 act = COPY;
1115 break;
1116 case 'r':
1118 * interactively rename files
1120 iflag = 1;
1121 break;
1122 case 's':
1124 * swap bytes after reading data
1126 break;
1127 case 't':
1129 * list contents of archive
1131 act = LIST;
1132 listf = stdout;
1133 break;
1134 case 'u':
1136 * replace newer files
1138 kflag = 0;
1139 break;
1140 case 'v':
1142 * verbose operation mode
1144 vflag = 1;
1145 break;
1146 case 'z':
1148 * use gzip. Non standard option.
1150 gzip_program = GZIP_CMD;
1151 break;
1152 case 'A':
1154 * append mode
1156 act = APPND;
1157 break;
1158 case 'B':
1160 * Use 5120 byte block size
1162 wrblksz = 5120;
1163 break;
1164 case 'C':
1166 * set block size in bytes
1168 wrblksz = atoi(optarg);
1169 break;
1170 case 'E':
1172 * file with patterns to extract or list
1174 if ((fp = fopen(optarg, "r")) == NULL) {
1175 paxwarn(1, "Unable to open file '%s' for read", optarg);
1176 cpio_usage();
1178 while ((str = getline(fp)) != NULL) {
1179 pat_add(str, NULL);
1181 fclose(fp);
1182 if (getline_error) {
1183 paxwarn(1, "Problem with file '%s'", optarg);
1184 cpio_usage();
1186 break;
1187 case 'F':
1188 case 'I':
1189 case 'O':
1191 * filename where the archive is stored
1193 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1195 * treat a - as stdin
1197 arcname = NULL;
1198 break;
1200 arcname = optarg;
1201 break;
1202 case 'H':
1204 * specify an archive format on write
1206 tmp.name = optarg;
1207 if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1208 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL)
1209 break;
1210 paxwarn(1, "Unknown -H format: %s", optarg);
1211 (void)fputs("cpio: Known -H formats are:", stderr);
1212 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1213 (void)fprintf(stderr, " %s", fsub[i].name);
1214 (void)fputs("\n\n", stderr);
1215 cpio_usage();
1216 break;
1217 case 'L':
1219 * follow symbolic links
1221 Lflag = 1;
1222 break;
1223 case 'S':
1225 * swap halfwords after reading data
1227 break;
1228 case 'Z':
1230 * use compress. Non standard option.
1232 gzip_program = COMPRESS_CMD;
1233 break;
1234 case '6':
1236 * process Version 6 cpio format
1238 frmt = &(fsub[F_OCPIO]);
1239 break;
1240 case '?':
1241 default:
1242 cpio_usage();
1243 break;
1245 argc -= optind;
1246 argv += optind;
1249 * process the args as they are interpreted by the operation mode
1251 switch (act) {
1252 case LIST:
1253 case EXTRACT:
1254 while (*argv != NULL)
1255 if (pat_add(*argv++, NULL) < 0)
1256 cpio_usage();
1257 break;
1258 case COPY:
1259 if (*argv == NULL) {
1260 paxwarn(0, "Destination directory was not supplied");
1261 cpio_usage();
1263 dirptr = *argv;
1264 if (mkpath(dirptr) < 0)
1265 cpio_usage();
1266 --argc;
1267 ++argv;
1268 /* FALLTHROUGH */
1269 case ARCHIVE:
1270 case APPND:
1271 if (*argv != NULL)
1272 cpio_usage();
1274 * no read errors allowed on updates/append operation!
1276 maxflt = 0;
1277 while ((str = getline(stdin)) != NULL) {
1278 ftree_add(str, 0);
1280 if (getline_error) {
1281 paxwarn(1, "Problem while reading stdin");
1282 cpio_usage();
1284 break;
1285 default:
1286 cpio_usage();
1287 break;
1292 * printflg()
1293 * print out those invalid flag sets found to the user
1296 static void
1297 printflg(unsigned int flg)
1299 int nxt;
1300 int pos = 0;
1302 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1303 while ((nxt = ffs(flg)) != 0) {
1304 flg = flg >> nxt;
1305 pos += nxt;
1306 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1308 (void)putc('\n', stderr);
1312 * c_frmt()
1313 * comparison routine used by bsearch to find the format specified
1314 * by the user
1317 static int
1318 c_frmt(const void *a, const void *b)
1320 return(strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name));
1324 * opt_next()
1325 * called by format specific options routines to get each format specific
1326 * flag and value specified with -o
1327 * Return:
1328 * pointer to next OPLIST entry or NULL (end of list).
1331 OPLIST *
1332 opt_next(void)
1334 OPLIST *opt;
1336 if ((opt = ophead) != NULL)
1337 ophead = ophead->fow;
1338 return(opt);
1342 * bad_opt()
1343 * generic routine used to complain about a format specific options
1344 * when the format does not support options.
1348 bad_opt(void)
1350 OPLIST *opt;
1352 if (ophead == NULL)
1353 return(0);
1355 * print all we were given
1357 paxwarn(1,"These format options are not supported");
1358 while ((opt = opt_next()) != NULL)
1359 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1360 pax_usage();
1361 return(0);
1365 * opt_add()
1366 * breaks the value supplied to -o into an option name and value. Options
1367 * are given to -o in the form -o name-value,name=value
1368 * multiple -o may be specified.
1369 * Return:
1370 * 0 if format in name=value format, -1 if -o is passed junk.
1374 opt_add(const char *str)
1376 OPLIST *opt;
1377 char *frpt;
1378 char *pt;
1379 char *endpt;
1380 char *lstr;
1382 if ((str == NULL) || (*str == '\0')) {
1383 paxwarn(0, "Invalid option name");
1384 return(-1);
1386 if ((lstr = strdup(str)) == NULL) {
1387 paxwarn(0, "Unable to allocate space for option list");
1388 return(-1);
1390 frpt = endpt = lstr;
1393 * break into name and values pieces and stuff each one into a
1394 * OPLIST structure. When we know the format, the format specific
1395 * option function will go through this list
1397 while ((frpt != NULL) && (*frpt != '\0')) {
1398 if ((endpt = strchr(frpt, ',')) != NULL)
1399 *endpt = '\0';
1400 if ((pt = strchr(frpt, '=')) == NULL) {
1401 paxwarn(0, "Invalid options format");
1402 free(lstr);
1403 return(-1);
1405 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1406 paxwarn(0, "Unable to allocate space for option list");
1407 free(lstr);
1408 return(-1);
1410 *pt++ = '\0';
1411 opt->name = frpt;
1412 opt->value = pt;
1413 opt->fow = NULL;
1414 if (endpt != NULL)
1415 frpt = endpt + 1;
1416 else
1417 frpt = NULL;
1418 if (ophead == NULL) {
1419 optail = ophead = opt;
1420 continue;
1422 optail->fow = opt;
1423 optail = opt;
1425 return(0);
1429 * str_offt()
1430 * Convert an expression of the following forms to an off_t > 0.
1431 * 1) A positive decimal number.
1432 * 2) A positive decimal number followed by a b (mult by 512).
1433 * 3) A positive decimal number followed by a k (mult by 1024).
1434 * 4) A positive decimal number followed by a m (mult by 512).
1435 * 5) A positive decimal number followed by a w (mult by sizeof int)
1436 * 6) Two or more positive decimal numbers (with/without k,b or w).
1437 * separated by x (also * for backwards compatibility), specifying
1438 * the product of the indicated values.
1439 * Return:
1440 * 0 for an error, a positive value o.w.
1443 static off_t
1444 str_offt(char *val)
1446 char *expr;
1447 off_t num, t;
1449 # ifdef NET2_STAT
1450 num = strtol(val, &expr, 0);
1451 if ((num == LONG_MAX) || (num <= 0) || (expr == val))
1452 # else
1453 num = strtoq(val, &expr, 0);
1454 if ((num == QUAD_MAX) || (num <= 0) || (expr == val))
1455 # endif
1456 return(0);
1458 switch(*expr) {
1459 case 'b':
1460 t = num;
1461 num *= 512;
1462 if (t > num)
1463 return(0);
1464 ++expr;
1465 break;
1466 case 'k':
1467 t = num;
1468 num *= 1024;
1469 if (t > num)
1470 return(0);
1471 ++expr;
1472 break;
1473 case 'm':
1474 t = num;
1475 num *= 1048576;
1476 if (t > num)
1477 return(0);
1478 ++expr;
1479 break;
1480 case 'w':
1481 t = num;
1482 num *= sizeof(int);
1483 if (t > num)
1484 return(0);
1485 ++expr;
1486 break;
1489 switch(*expr) {
1490 case '\0':
1491 break;
1492 case '*':
1493 case 'x':
1494 t = num;
1495 num *= str_offt(expr + 1);
1496 if (t > num)
1497 return(0);
1498 break;
1499 default:
1500 return(0);
1502 return(num);
1505 char *fgetln(FILE *f, size_t *);
1507 static char *
1508 getline(FILE *f)
1510 char *name, *temp;
1511 size_t len;
1513 name = fgetln(f, &len);
1514 if (!name) {
1515 getline_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
1516 return(0);
1518 if (name[len-1] != '\n')
1519 len++;
1520 temp = malloc(len);
1521 if (!temp) {
1522 getline_error = GETLINE_OUT_OF_MEM;
1523 return(0);
1525 memcpy(temp, name, len-1);
1526 temp[len-1] = 0;
1527 return(temp);
1531 * no_op()
1532 * for those option functions where the archive format has nothing to do.
1533 * Return:
1537 static int
1538 no_op(void)
1540 return(0);
1544 * pax_usage()
1545 * print the usage summary to the user
1548 static void
1549 pax_usage(void)
1551 (void)fputs("usage: pax [-cdnvz] [-E limit] [-f archive] ", stderr);
1552 (void)fputs("[-s replstr] ... [-U user] ...", stderr);
1553 (void)fputs("\n [-G group] ... ", stderr);
1554 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1555 (void)fputs("[pattern ...]\n", stderr);
1556 (void)fputs(" pax -r [-cdiknuvzDYZ] [-E limit] ", stderr);
1557 (void)fputs("[-f archive] [-o options] ... \n", stderr);
1558 (void)fputs(" [-p string] ... [-s replstr] ... ", stderr);
1559 (void)fputs("[-U user] ... [-G group] ...\n ", stderr);
1560 (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
1561 (void)fputs(" [pattern ...]\n", stderr);
1562 (void)fputs(" pax -w [-dituvzHLPX] [-b blocksize] ", stderr);
1563 (void)fputs("[ [-a] [-f archive] ] [-x format] \n", stderr);
1564 (void)fputs(" [-B bytes] [-s replstr] ... ", stderr);
1565 (void)fputs("[-o options] ... [-U user] ...", stderr);
1566 (void)fputs("\n [-G group] ... ", stderr);
1567 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1568 (void)fputs("[file ...]\n", stderr);
1569 (void)fputs(" pax -r -w [-diklntuvDHLPXYZ] ", stderr);
1570 (void)fputs("[-p string] ... [-s replstr] ...", stderr);
1571 (void)fputs("\n [-U user] ... [-G group] ... ", stderr);
1572 (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
1573 (void)fputs("\n [file ...] directory\n", stderr);
1574 exit(1);
1578 * tar_usage()
1579 * print the usage summary to the user
1582 static void
1583 tar_usage(void)
1585 (void)fputs("usage: tar [-]{crtux}[-befhjmopqsvwyzHLOPXZ014578] [blocksize] ",
1586 stderr);
1587 (void)fputs("[archive] [replstr] [-C directory] [-I file] [file ...]\n",
1588 stderr);
1589 exit(1);
1593 * cpio_usage()
1594 * print the usage summary to the user
1597 static void
1598 cpio_usage(void)
1600 (void)fputs("usage: cpio -o [-aABcLvVzZ] [-C bytes] [-H format] [-O archive]\n", stderr);
1601 (void)fputs(" [-F archive] < name-list [> archive]\n", stderr);
1602 (void)fputs(" cpio -i [-bBcdfmnrsStuvVzZ6] [-C bytes] [-E file] [-H format]\n", stderr);
1603 (void)fputs(" [-I archive] [-F archive] [pattern...] [< archive]\n", stderr);
1604 (void)fputs(" cpio -p [-adlLmuvV] destination-directory < name-list\n", stderr);
1605 exit(1);