1 /* $NetBSD: options.c,v 1.116 2015/04/11 15:41:33 christos Exp $ */
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
40 #include <sys/cdefs.h>
43 static char sccsid
[] = "@(#)options.c 8.2 (Berkeley) 4/18/94";
45 __RCSID("$NetBSD: options.c,v 1.116 2015/04/11 15:41:33 christos Exp $");
49 #include <sys/types.h>
52 #include <sys/param.h>
55 #if HAVE_NBTOOL_CONFIG_H
56 #include "compat_getopt.h"
77 * Routines which handle command line options
80 static int nopids
; /* tar mode: suppress "pids" for -p option */
81 static char flgch
[] = FLGCH
; /* list of all possible flags (pax) */
82 static OPLIST
*ophead
= NULL
; /* head for format specific options -x */
83 static OPLIST
*optail
= NULL
; /* option tail */
85 static int opt_add(const char *);
86 static int no_op(void);
87 static void printflg(unsigned int);
88 static int c_frmt(const void *, const void *);
89 static off_t
str_offt(char *);
90 static char *get_line(FILE *fp
);
91 static void pax_options(int, char **);
92 __dead
static void pax_usage(void);
93 static void tar_options(int, char **);
94 __dead
static void tar_usage(void);
96 static void cpio_options(int, char **);
97 __dead
static void cpio_usage(void);
100 /* errors from get_line */
101 #define GETLINE_FILE_CORRUPT 1
102 #define GETLINE_OUT_OF_MEM 2
103 static int get_line_error
;
105 #define BZIP2_CMD "bzip2" /* command to run as bzip2 */
106 #define GZIP_CMD "gzip" /* command to run as gzip */
107 #define XZ_CMD "xz" /* command to run as xz */
108 #define COMPRESS_CMD "compress" /* command to run as compress */
113 #define OPT_USE_COMPRESS_PROGRAM 0
114 #define OPT_CHECKPOINT 1
117 #define OPT_ATIME_PRESERVE 4
118 #define OPT_IGNORE_FAILED_READ 5
119 #define OPT_REMOVE_FILES 6
122 #define OPT_VERSION 9
123 #define OPT_EXCLUDE 10
124 #define OPT_BLOCK_COMPRESS 11
125 #define OPT_NORECURSE 12
126 #define OPT_FORCE_LOCAL 13
127 #define OPT_INSECURE 14
128 #define OPT_STRICT 15
129 #define OPT_SPARSE 16
132 #if !HAVE_NBTOOL_CONFIG_H
133 #define OPT_CHROOT 19
137 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
138 * (see pax.h for description of each function)
140 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
141 * read, end_read, st_write, write, end_write, trail,
142 * subtrail, rd_data, wr_data, options
147 /* 0: OLD BINARY CPIO */
148 { "bcpio", 5120, sizeof(HD_BCPIO
), 1, 0, 0, 1, bcpio_id
, cpio_strd
,
149 bcpio_rd
, bcpio_endrd
, cpio_stwr
, bcpio_wr
, cpio_endwr
, NULL
,
150 cpio_subtrail
, rd_wrfile
, wr_rdfile
, bad_opt
},
152 /* 1: OLD OCTAL CHARACTER CPIO */
153 { "cpio", 5120, sizeof(HD_CPIO
), 1, 0, 0, 1, cpio_id
, cpio_strd
,
154 cpio_rd
, cpio_endrd
, cpio_stwr
, cpio_wr
, cpio_endwr
, NULL
,
155 cpio_subtrail
, rd_wrfile
, wr_rdfile
, bad_opt
},
157 /* 2: SVR4 HEX CPIO */
158 { "sv4cpio", 5120, sizeof(HD_VCPIO
), 1, 0, 0, 1, vcpio_id
, cpio_strd
,
159 vcpio_rd
, vcpio_endrd
, cpio_stwr
, vcpio_wr
, cpio_endwr
, NULL
,
160 cpio_subtrail
, rd_wrfile
, wr_rdfile
, bad_opt
},
162 /* 3: SVR4 HEX CPIO WITH CRC */
163 { "sv4crc", 5120, sizeof(HD_VCPIO
), 1, 0, 0, 1, crc_id
, crc_strd
,
164 vcpio_rd
, vcpio_endrd
, crc_stwr
, vcpio_wr
, cpio_endwr
, NULL
,
165 cpio_subtrail
, rd_wrfile
, wr_rdfile
, bad_opt
},
168 { "tar", 10240, BLKMULT
, 0, 1, BLKMULT
, 0, tar_id
, no_op
,
169 tar_rd
, tar_endrd
, no_op
, tar_wr
, tar_endwr
, tar_trail
,
170 NULL
, rd_wrfile
, wr_rdfile
, tar_opt
},
173 { "ustar", 10240, BLKMULT
, 0, 1, BLKMULT
, 0, ustar_id
, ustar_strd
,
174 ustar_rd
, tar_endrd
, ustar_stwr
, ustar_wr
, tar_endwr
, tar_trail
,
175 NULL
, rd_wrfile
, wr_rdfile
, bad_opt
}
178 #define F_BCPIO 0 /* old binary cpio format */
179 #define F_CPIO 1 /* old octal character cpio format */
180 #define F_SV4CPIO 2 /* SVR4 hex cpio format */
181 #define F_SV4CRC 3 /* SVR4 hex with crc cpio format */
182 #define F_TAR 4 /* old V7 UNIX tar format */
183 #define F_USTAR 5 /* ustar format */
185 #define F_TAR 0 /* old V7 UNIX tar format */
186 #define F_USTAR 1 /* ustar format */
188 #define DEFLT F_USTAR /* default write format from list above */
191 * ford is the archive search order used by get_arc() to determine what kind
192 * of archive we are dealing with. This helps to properly id archive formats
193 * some formats may be subsets of others....
195 int ford
[] = {F_USTAR
, F_TAR
,
197 F_SV4CRC
, F_SV4CPIO
, F_CPIO
, F_BCPIO
,
202 * filename record separator
207 * Do we have -C anywhere?
213 * figure out if we are pax, tar or cpio. Call the appropriate options
218 options(int argc
, char **argv
)
222 * Are we acting like pax, tar or cpio (based on argv[0])
224 if ((argv0
= strrchr(argv
[0], '/')) != NULL
)
229 if (strstr(argv0
, NM_TAR
)) {
231 tar_options(argc
, argv
);
233 } else if (strstr(argv0
, NM_CPIO
)) {
235 cpio_options(argc
, argv
);
239 pax_options(argc
, argv
);
243 struct option pax_longopts
[] = {
244 { "insecure", no_argument
, 0,
246 { "force-local", no_argument
, 0,
248 { "use-compress-program", required_argument
, 0,
249 OPT_USE_COMPRESS_PROGRAM
},
250 { "xz", no_argument
, 0,
252 { "gnu", no_argument
, 0,
260 * look at the user specified flags. set globals as required and check if
261 * the user specified a legal set of flags. If not, complain and exit
265 pax_options(int argc
, char **argv
)
275 * process option flags
277 while ((c
= getopt_long(argc
, argv
,
278 "0ab:cdf:ijklno:p:rs:tuvwx:zAB:DE:G:HLMN:OPT:U:VXYZ",
279 pax_longopts
, NULL
)) != -1) {
295 if ((wrblksz
= (int)str_offt(optarg
)) <= 0) {
296 tty_warn(1, "Invalid block size %s", optarg
);
302 * inverse match on patterns
309 * match only dir on extract, not the subtree at dir
316 * filename where the archive is stored
323 * interactive file rename
332 gzip_program
= BZIP2_CMD
;
336 * do not clobber files that exist
343 * try to link src to dest with copy (-rw)
350 * select first match for a pattern only
357 * pass format specific options
360 if (opt_add(optarg
) < 0)
365 * specify file characteristic options
367 for (pt
= optarg
; *pt
!= '\0'; ++pt
) {
371 * do not preserve access time
377 * preserve user id, group id, file
378 * mode, access/modification times
390 * do not preserve file flags
397 * do not preserve modification time
409 * preserve file mode bits
414 tty_warn(1, "Invalid -p string: %c",
430 * file name substitution name pattern
432 if (rep_add(optarg
) < 0) {
440 * preserve access time on filesystem nodes we read
447 * ignore those older files
454 * verbose operation mode
467 * specify an archive format on write
470 frmt
= (FSUB
*)bsearch((void *)&tmp
, (void *)fsub
,
471 sizeof(fsub
)/sizeof(FSUB
), sizeof(FSUB
), c_frmt
);
476 tty_warn(1, "Unknown -x format: %s", optarg
);
477 (void)fputs("pax: Known -x formats are:", stderr
);
478 for (i
= 0; i
< (sizeof(fsub
)/sizeof(FSUB
)); ++i
)
479 (void)fprintf(stderr
, " %s", fsub
[i
].name
);
480 (void)fputs("\n\n", stderr
);
485 * use gzip. Non standard option.
487 gzip_program
= GZIP_CMD
;
495 * non-standard option on number of bytes written on a
496 * single archive volume.
498 if ((wrlimit
= str_offt(optarg
)) <= 0) {
499 tty_warn(1, "Invalid write limit %s", optarg
);
502 if (wrlimit
% BLKMULT
) {
504 "Write limit is not a %d byte multiple",
512 * On extraction check file inode change time before the
513 * modification of the file name. Non standard option.
520 * non-standard limit on read faults
521 * 0 indicates stop after first error, values
522 * indicate a limit, "none" try forever
525 if (strcmp(none
, optarg
) == 0)
527 else if ((maxflt
= atoi(optarg
)) < 0) {
529 "Error count value must be positive");
535 * non-standard option for selecting files within an
536 * archive by group (gid or name)
538 if (grp_add(optarg
) < 0) {
546 * follow command line symlinks only
561 tty_warn(1, "Support for -%c is not compiled in", c
);
566 * Treat list of filenames on stdin as an
567 * mtree(8) specfile. Non standard option.
574 * Use alternative directory for user db lookups.
576 if (!setup_getid(optarg
)) {
578 "Unable to use user and group databases in `%s'",
586 * Force one volume. Non standard option.
588 force_one_volume
= 1;
592 * do NOT follow symlinks (default)
599 * non-standard option for selecting files within an
600 * archive by modification time range (lower,upper)
602 if (trng_add(optarg
) < 0) {
610 * non-standard option for selecting files within an
611 * archive by user (uid or name)
613 if (usr_add(optarg
) < 0) {
621 * somewhat verbose operation mode (no listing)
628 * do not pass over mount points in the file system
635 * On extraction check file inode change time after the
636 * modification of the file name. Non standard option.
643 * On extraction check modification time after the
644 * modification of the file name. Non standard option.
652 case OPT_FORCE_LOCAL
:
655 case OPT_USE_COMPRESS_PROGRAM
:
656 gzip_program
= optarg
;
659 gzip_program
= XZ_CMD
;
672 * figure out the operation mode of pax read,write,extract,copy,append
673 * or list. check that we have not been given a bogus set of flags
674 * for the operation mode.
680 } else if (ISEXTRACT(flg
)) {
683 } else if (ISARCHIVE(flg
)) {
686 } else if (ISAPPND(flg
)) {
689 } else if (ISCOPY(flg
)) {
700 * if we are writing (ARCHIVE) we use the default format if the user
701 * did not specify a format. when we write during an APPEND, we will
702 * adopt the format of the existing archive if none was supplied.
704 if (!(flg
& XF
) && (act
== ARCHIVE
))
705 frmt
= &(fsub
[DEFLT
]);
708 * process the args as they are interpreted by the operation mode
713 for (; optind
< argc
; optind
++)
714 if (pat_add(argv
[optind
], NULL
, 0) < 0)
718 if (optind
>= argc
) {
719 tty_warn(0, "Destination directory was not supplied");
724 if (mkpath(dirptr
) < 0)
729 for (; optind
< argc
; optind
++)
730 if (ftree_add(argv
[optind
], 0) < 0)
733 * no read errors allowed on updates/append operation!
743 * look at the user specified flags. set globals as required and check if
744 * the user specified a legal set of flags. If not, complain and exit
747 struct option tar_longopts
[] = {
748 { "block-size", required_argument
, 0, 'b' },
749 { "bunzip2", no_argument
, 0, 'j' },
750 { "bzip2", no_argument
, 0, 'j' },
751 { "create", no_argument
, 0, 'c' }, /* F */
752 /* -e -- no corresponding long option */
753 { "file", required_argument
, 0, 'f' },
754 { "dereference", no_argument
, 0, 'h' },
755 { "keep-old-files", no_argument
, 0, 'k' },
756 { "one-file-system", no_argument
, 0, 'l' },
757 { "modification-time", no_argument
, 0, 'm' },
758 { "old-archive", no_argument
, 0, 'o' },
759 { "portability", no_argument
, 0, 'o' },
760 { "same-permissions", no_argument
, 0, 'p' },
761 { "preserve-permissions", no_argument
, 0, 'p' },
762 { "preserve", no_argument
, 0, 'p' },
763 { "fast-read", no_argument
, 0, 'q' },
764 { "append", no_argument
, 0, 'r' }, /* F */
765 { "update", no_argument
, 0, 'u' }, /* F */
766 { "list", no_argument
, 0, 't' }, /* F */
767 { "verbose", no_argument
, 0, 'v' },
768 { "interactive", no_argument
, 0, 'w' },
769 { "confirmation", no_argument
, 0, 'w' },
770 { "extract", no_argument
, 0, 'x' }, /* F */
771 { "get", no_argument
, 0, 'x' }, /* F */
772 { "gzip", no_argument
, 0, 'z' },
773 { "gunzip", no_argument
, 0, 'z' },
774 { "read-full-blocks", no_argument
, 0, 'B' },
775 { "directory", required_argument
, 0, 'C' },
776 { "xz", no_argument
, 0, 'J' },
777 { "to-stdout", no_argument
, 0, 'O' },
778 { "absolute-paths", no_argument
, 0, 'P' },
779 { "sparse", no_argument
, 0, 'S' },
780 { "files-from", required_argument
, 0, 'T' },
781 { "summary", no_argument
, 0, 'V' },
782 { "stats", no_argument
, 0, 'V' },
783 { "exclude-from", required_argument
, 0, 'X' },
784 { "compress", no_argument
, 0, 'Z' },
785 { "uncompress", no_argument
, 0, 'Z' },
786 { "strict", no_argument
, 0,
788 { "atime-preserve", no_argument
, 0,
789 OPT_ATIME_PRESERVE
},
790 { "unlink", no_argument
, 0,
792 { "use-compress-program", required_argument
, 0,
793 OPT_USE_COMPRESS_PROGRAM
},
794 { "force-local", no_argument
, 0,
796 { "insecure", no_argument
, 0,
798 { "exclude", required_argument
, 0,
800 { "no-recursion", no_argument
, 0,
802 #if !HAVE_NBTOOL_CONFIG_H
803 { "chroot", no_argument
, 0,
806 #if 0 /* Not implemented */
807 { "catenate", no_argument
, 0, 'A' }, /* F */
808 { "concatenate", no_argument
, 0, 'A' }, /* F */
809 { "diff", no_argument
, 0, 'd' }, /* F */
810 { "compare", no_argument
, 0, 'd' }, /* F */
811 { "checkpoint", no_argument
, 0,
813 { "help", no_argument
, 0,
815 { "info-script", required_argument
, 0, 'F' },
816 { "new-volume-script", required_argument
, 0, 'F' },
817 { "incremental", no_argument
, 0, 'G' },
818 { "listed-incremental", required_argument
, 0, 'g' },
819 { "ignore-zeros", no_argument
, 0, 'i' },
820 { "ignore-failed-read", no_argument
, 0,
821 OPT_IGNORE_FAILED_READ
},
822 { "starting-file", no_argument
, 0, 'K' },
823 { "tape-length", required_argument
, 0, 'L' },
824 { "multi-volume", no_argument
, 0, 'M' },
825 { "after-date", required_argument
, 0, 'N' },
826 { "newer", required_argument
, 0, 'N' },
827 { "record-number", no_argument
, 0, 'R' },
828 { "remove-files", no_argument
, 0,
830 { "same-order", no_argument
, 0, 's' },
831 { "preserve-order", no_argument
, 0, 's' },
832 { "null", no_argument
, 0,
834 { "totals", no_argument
, 0,
836 { "volume-name", required_argument
, 0, 'V' }, /* XXX */
837 { "label", required_argument
, 0, 'V' }, /* XXX */
838 { "version", no_argument
, 0,
840 { "verify", no_argument
, 0, 'W' },
841 { "block-compress", no_argument
, 0,
842 OPT_BLOCK_COMPRESS
},
848 tar_set_action(int op
)
850 if (act
!= ERROR
&& act
!= op
)
856 tar_options(int argc
, char **argv
)
862 int incfiles_max
= 0;
867 struct incfile
*incfiles
= NULL
;
870 * Set default values.
876 * process option flags
878 while ((c
= getoldopt(argc
, argv
,
879 "+b:cef:hjklmopqrs:tuvwxzBC:HI:JOPST:X:Z014578",
885 * specify blocksize in 512-byte blocks
887 if ((wrblksz
= (int)str_offt(optarg
)) <= 0) {
888 tty_warn(1, "Invalid block size %s", optarg
);
891 wrblksz
*= 512; /* XXX - check for int oflow */
897 tar_set_action(ARCHIVE
);
901 * stop after first error
907 * filename where the archive is stored
909 if ((optarg
[0] == '-') && (optarg
[1]== '\0')) {
928 * pass through bzip2. not a standard option
930 gzip_program
= BZIP2_CMD
;
934 * do not clobber files that exist
940 * do not pass over mount points in the file system
946 * do not preserve modification time
952 * This option does several things based on whether
953 * this is a create or extract operation.
955 if (act
== ARCHIVE
) {
956 /* GNU tar: write V7 format archives. */
958 /* 4.2BSD: don't add directory entries. */
959 if (opt_add("write_opt=nodir") < 0)
963 /* SUS: don't preserve owner/group. */
970 * preserve user id, group id, file
971 * mode, access/modification times
981 * select first match for a pattern only
988 * append to the archive
990 tar_set_action(APPND
);
994 * file name substitution name pattern
996 if (rep_add(optarg
) < 0) {
1003 * list contents of the tape
1005 tar_set_action(LIST
);
1009 * verbose operation mode
1015 * interactive file rename
1021 * extract an archive, preserving mode,
1022 * and mtime if possible.
1024 tar_set_action(EXTRACT
);
1029 * use gzip. Non standard option.
1031 gzip_program
= GZIP_CMD
;
1035 * Nothing to do here, this is pax default
1044 * follow command line symlinks only
1050 if (++nincfiles
> incfiles_max
) {
1051 incfiles_max
= nincfiles
+ 3;
1052 incfiles
= realloc(incfiles
,
1053 sizeof(*incfiles
) * incfiles_max
);
1054 if (incfiles
== NULL
) {
1055 tty_warn(0, "Unable to allocate space "
1060 incfiles
[nincfiles
- 1].file
= optarg
;
1061 incfiles
[nincfiles
- 1].dir
= chdname
;
1064 gzip_program
= XZ_CMD
;
1071 * do not remove leading '/' from pathnames
1077 /* do nothing; we already generate sparse files */
1081 * semi-verbose operation mode (no listing)
1087 * GNU tar compat: exclude the files listed in optarg
1089 if (tar_gnutar_X_compat(optarg
) != 0)
1096 gzip_program
= COMPRESS_CMD
;
1116 case OPT_ATIME_PRESERVE
:
1120 /* Just ignore -- we always unlink first. */
1122 case OPT_USE_COMPRESS_PROGRAM
:
1123 gzip_program
= optarg
;
1125 case OPT_FORCE_LOCAL
:
1132 /* disable gnu extensions */
1136 if (tar_gnutar_minus_minus_exclude(optarg
) != 0)
1142 #if !HAVE_NBTOOL_CONFIG_H
1155 /* Tar requires an action. */
1159 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
1160 if (fstdin
== 1 && act
== ARCHIVE
)
1165 /* Traditional tar behaviour (pax wants to read file list from stdin) */
1166 if ((act
== ARCHIVE
|| act
== APPND
) && argc
== 0 && nincfiles
== 0)
1169 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
1170 * (unless -o specified)
1172 if (act
== ARCHIVE
|| act
== APPND
)
1173 frmt
= &(fsub
[Oflag
? F_TAR
: F_USTAR
]);
1178 tty_warn(1, "The -O/-o options are only valid when "
1179 "writing or extracting an archive");
1185 * process the args as they are interpreted by the operation mode
1194 char *file
, *dir
= NULL
;
1195 int mustfreedir
= 0;
1197 while (nincfiles
|| *argv
!= NULL
) {
1199 * If we queued up any include files,
1200 * pull them in now. Otherwise, check
1201 * for -I and -C positional flags.
1202 * Anything else must be a file to
1206 file
= incfiles
->file
;
1207 dir
= incfiles
->dir
;
1211 } else if (strcmp(*argv
, "-I") == 0) {
1212 if (*++argv
== NULL
)
1226 if (strcmp(file
, "-") == 0)
1228 else if ((fp
= fopen(file
, "r")) == NULL
) {
1229 tty_warn(1, "Unable to open file '%s' for read", file
);
1232 while ((str
= get_line(fp
)) != NULL
) {
1234 if (dir
&& mustfreedir
)
1241 if (strcmp(str
, "-C") == 0) {
1247 if (strncmp(str
, "-C ", 3) == 0) {
1249 if (dir
&& mustfreedir
)
1251 dir
= strdup(str
+ 3);
1256 if (pat_add(str
, dir
, NOGLOB_MTCH
) < 0)
1260 /* Bomb if given -C w/out a dir. */
1263 if (dir
&& mustfreedir
)
1265 if (strcmp(file
, "-") != 0)
1267 if (get_line_error
) {
1268 tty_warn(1, "Problem with file '%s'", file
);
1271 } else if (strcmp(*argv
, "-C") == 0) {
1272 if (*++argv
== NULL
)
1276 } else if (pat_add(*argv
++, chdname
, 0) < 0)
1282 * if patterns were added, we are doing chdir()
1283 * on a file-by-file basis, else, just one
1284 * global chdir (if any) after opening input.
1292 if (chdname
!= NULL
) { /* initial chdir() */
1293 if (ftree_add(chdname
, 1) < 0)
1297 while (nincfiles
|| *argv
!= NULL
) {
1301 * If we queued up any include files, pull them in
1302 * now. Otherwise, check for -I and -C positional
1303 * flags. Anything else must be a file to include
1307 file
= incfiles
->file
;
1308 dir
= incfiles
->dir
;
1311 } else if (strcmp(*argv
, "-I") == 0) {
1312 if (*++argv
== NULL
)
1325 /* Set directory if needed */
1327 if (ftree_add(dir
, 1) < 0)
1331 if (strcmp(file
, "-") == 0)
1333 else if ((fp
= fopen(file
, "r")) == NULL
) {
1334 tty_warn(1, "Unable to open file '%s' for read", file
);
1337 while ((str
= get_line(fp
)) != NULL
) {
1339 if (ftree_add(str
, 1) < 0)
1344 if (strcmp(str
, "-C") == 0) {
1348 if (strncmp(str
, "-C ", 3) == 0) {
1349 if (ftree_add(str
+ 3, 1) < 0)
1353 if (ftree_add(str
, 0) < 0)
1356 /* Bomb if given -C w/out a dir. */
1359 if (strcmp(file
, "-") != 0)
1361 if (get_line_error
) {
1362 tty_warn(1, "Problem with file '%s'",
1366 } else if (strcmp(*argv
, "-C") == 0) {
1367 if (*++argv
== NULL
)
1369 if (ftree_add(*argv
++, 1) < 0)
1371 } else if (ftree_add(*argv
++, 0) < 0)
1375 * no read errors allowed on updates/append operation!
1380 if (!fstdin
&& ((arcname
== NULL
) || (*arcname
== '\0'))) {
1381 arcname
= getenv("TAPE");
1382 if ((arcname
== NULL
) || (*arcname
== '\0'))
1383 arcname
= _PATH_DEFTAPE
;
1396 slash
+= strspn(slash
, "/");
1397 slash
+= strcspn(slash
, "/");
1399 done
= (*slash
== '\0');
1402 if (domkdir(path
, 0777) == -1)
1411 /* Can't create or or not a directory */
1412 syswarn(1, errno
, "Cannot create directory `%s'", path
);
1418 struct option cpio_longopts
[] = {
1419 { "reset-access-time", no_argument
, 0, 'a' },
1420 { "make-directories", no_argument
, 0, 'd' },
1421 { "nonmatching", no_argument
, 0, 'f' },
1422 { "extract", no_argument
, 0, 'i' },
1423 { "link", no_argument
, 0, 'l' },
1424 { "preserve-modification-time", no_argument
, 0, 'm' },
1425 { "create", no_argument
, 0, 'o' },
1426 { "pass-through", no_argument
, 0, 'p' },
1427 { "rename", no_argument
, 0, 'r' },
1428 { "list", no_argument
, 0, 't' },
1429 { "unconditional", no_argument
, 0, 'u' },
1430 { "verbose", no_argument
, 0, 'v' },
1431 { "append", no_argument
, 0, 'A' },
1432 { "pattern-file", required_argument
, 0, 'E' },
1433 { "file", required_argument
, 0, 'F' },
1434 { "force-local", no_argument
, 0,
1436 { "format", required_argument
, 0, 'H' },
1437 { "dereference", no_argument
, 0, 'L' },
1438 { "swap-halfwords", no_argument
, 0, 'S' },
1439 { "summary", no_argument
, 0, 'V' },
1440 { "stats", no_argument
, 0, 'V' },
1441 { "insecure", no_argument
, 0,
1443 { "sparse", no_argument
, 0,
1445 { "xz", no_argument
, 0,
1449 /* Not implemented */
1450 { "null", no_argument
, 0, '0' },
1451 { "swap", no_argument
, 0, 'b' },
1452 { "numeric-uid-gid", no_argument
, 0, 'n' },
1453 { "swap-bytes", no_argument
, 0, 's' },
1454 { "message", required_argument
, 0, 'M' },
1455 { "owner", required_argument
, 0 'R
' },
1456 { "dot", no_argument, 0, 'V
' }, /* xxx */
1457 { "block-size", required_argument, 0,
1459 { "no-absolute-pathnames", no_argument, 0,
1460 OPT_NO_ABSOLUTE_PATHNAMES },
1461 { "no-preserve-owner", no_argument, 0,
1462 OPT_NO_PRESERVE_OWNER },
1463 { "only-verify-crc", no_argument, 0,
1464 OPT_ONLY_VERIFY_CRC },
1465 { "rsh-command", required_argument, 0,
1467 { "version", no_argument, 0,
1474 cpio_set_action(int op)
1476 if ((act == APPND && op == ARCHIVE) || (act == ARCHIVE && op == APPND))
1478 else if (act == EXTRACT && op == LIST)
1480 else if (act != ERROR && act != op)
1488 * look at the user specified flags. set globals as required and check if
1489 * the user specified a legal set of flags. If not, complain and exit
1493 cpio_options(int argc, char **argv)
1512 * process option flags
1514 while ((c = getoldopt(argc, argv,
1515 "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
1516 cpio_longopts, NULL)) != -1) {
1520 * preserve access time on filesystem nodes we read
1528 * swap bytes and half-words when reading data
1536 frmt = &fsub[F_SV4CPIO];
1540 * create directories as needed
1541 * pax does this by default ..
1547 * inverse match on patterns
1556 cpio_set_action(EXTRACT);
1565 * try to link src to dest with copy (-rw)
1581 cpio_set_action(ARCHIVE);
1582 frmt = &(fsub[F_SV4CRC]);
1587 * cpio -p is like pax -rw
1589 cpio_set_action(COPY);
1594 * interactive file rename
1602 * swap bytes after reading data
1608 * list contents of archive
1610 cpio_set_action(LIST);
1616 * don't ignore those older files
1624 * verbose operation mode
1631 * use gzip. Non standard option.
1633 gzip_program
= GZIP_CMD
;
1637 * append to an archive
1639 cpio_set_action(APPND
);
1644 * set blocksize to 5120
1652 if ((blksz
= (int)str_offt(optarg
)) <= 0) {
1653 tty_warn(1, "Invalid block size %s", optarg
);
1659 * file with patterns to extract or list
1661 if ((fp
= fopen(optarg
, "r")) == NULL
) {
1662 tty_warn(1, "Unable to open file '%s' for read",
1666 while ((str
= get_line(fp
)) != NULL
) {
1667 pat_add(str
, NULL
, 0);
1670 if (get_line_error
) {
1671 tty_warn(1, "Problem with file '%s'", optarg
);
1677 * specify an archive format on write
1680 frmt
= (FSUB
*)bsearch((void *)&tmp
, (void *)fsub
,
1681 sizeof(fsub
)/sizeof(FSUB
), sizeof(FSUB
), c_frmt
);
1686 tty_warn(1, "Unknown -H format: %s", optarg
);
1687 (void)fputs("cpio: Known -H formats are:", stderr
);
1688 for (i
= 0; i
< (sizeof(fsub
)/sizeof(FSUB
)); ++i
)
1689 (void)fprintf(stderr
, " %s", fsub
[i
].name
);
1690 (void)fputs("\n\n", stderr
);
1697 * filename where the archive is stored
1699 if ((optarg
[0] == '-') && (optarg
[1]== '\0')) {
1701 * treat a - as stdin
1725 * swap halfwords after reading data
1730 case 'V': /* print a '.' for each file processed */
1735 * semi-verbose operation mode (no listing)
1742 * use compress. Non standard option.
1744 gzip_program
= COMPRESS_CMD
;
1748 * process Version 6 cpio format
1750 frmt
= &(fsub
[F_BCPIO
]);
1752 case OPT_FORCE_LOCAL
:
1759 /* do nothing; we already generate sparse files */
1762 gzip_program
= XZ_CMD
;
1771 * figure out the operation mode of cpio. check that we have not been
1772 * given a bogus set of flags for the operation mode.
1776 bflg
= flg
& BDLIST
;
1777 } else if (ISEXTRACT(flg
)) {
1779 bflg
= flg
& BDEXTR
;
1780 } else if (ISARCHIVE(flg
)) {
1782 bflg
= flg
& BDARCH
;
1783 } else if (ISAPPND(flg
)) {
1785 bflg
= flg
& BDARCH
;
1786 } else if (ISCOPY(flg
)) {
1788 bflg
= flg
& BDCOPY
;
1796 * if we are writing (ARCHIVE) we use the default format if the user
1797 * did not specify a format. when we write during an APPEND, we will
1798 * adopt the format of the existing archive if none was supplied.
1800 if (!(flg
& XF
) && (act
== ARCHIVE
))
1801 frmt
= &(fsub
[F_BCPIO
]);
1804 * process the args as they are interpreted by the operation mode
1809 for (; optind
< argc
; optind
++)
1810 if (pat_add(argv
[optind
], NULL
, 0) < 0)
1814 if (optind
>= argc
) {
1815 tty_warn(0, "Destination directory was not supplied");
1819 dirptr
= argv
[argc
];
1823 if (argc
!= optind
) {
1824 for (; optind
< argc
; optind
++)
1825 if (ftree_add(argv
[optind
], 0) < 0)
1830 * no read errors allowed on updates/append operation!
1833 while ((str
= get_line(stdin
)) != NULL
) {
1836 if (get_line_error
) {
1837 tty_warn(1, "Problem while reading stdin");
1850 * print out those invalid flag sets found to the user
1854 printflg(unsigned int flg
)
1858 (void)fprintf(stderr
,"%s: Invalid combination of options:", argv0
);
1859 while ((nxt
= ffs(flg
)) != 0) {
1860 flg
&= ~(1 << (nxt
- 1));
1861 (void)fprintf(stderr
, " -%c", flgch
[nxt
- 1]);
1863 (void)putc('\n', stderr
);
1868 * comparison routine used by bsearch to find the format specified
1873 c_frmt(const void *a
, const void *b
)
1875 return strcmp(((const FSUB
*)a
)->name
, ((const FSUB
*)b
)->name
);
1880 * called by format specific options routines to get each format specific
1881 * flag and value specified with -o
1883 * pointer to next OPLIST entry or NULL (end of list).
1891 if ((opt
= ophead
) != NULL
)
1892 ophead
= ophead
->fow
;
1898 * generic routine used to complain about a format specific options
1899 * when the format does not support options.
1910 * print all we were given
1912 tty_warn(1," These format options are not supported for %s",
1914 while ((opt
= opt_next()) != NULL
)
1915 (void)fprintf(stderr
, "\t%s = %s\n", opt
->name
, opt
->value
);
1916 if (strcmp(NM_TAR
, argv0
) == 0)
1919 else if (strcmp(NM_CPIO
, argv0
) == 0)
1929 * breaks the value supplied to -o into a option name and value. options
1930 * are given to -o in the form -o name-value,name=value
1931 * multiple -o may be specified.
1933 * 0 if format in name=value format, -1 if -o is passed junk
1937 opt_add(const char *str
)
1945 if ((str
== NULL
) || (*str
== '\0')) {
1946 tty_warn(0, "Invalid option name");
1949 if ((dstr
= strdup(str
)) == NULL
) {
1950 tty_warn(0, "Unable to allocate space for option list");
1953 frpt
= endpt
= dstr
;
1956 * break into name and values pieces and stuff each one into a
1957 * OPLIST structure. When we know the format, the format specific
1958 * option function will go through this list
1960 while ((frpt
!= NULL
) && (*frpt
!= '\0')) {
1961 if ((endpt
= strchr(frpt
, ',')) != NULL
)
1963 if ((pt
= strchr(frpt
, '=')) == NULL
) {
1964 tty_warn(0, "Invalid options format");
1968 if ((opt
= (OPLIST
*)malloc(sizeof(OPLIST
))) == NULL
) {
1969 tty_warn(0, "Unable to allocate space for option list");
1981 if (ophead
== NULL
) {
1982 optail
= ophead
= opt
;
1993 * Convert an expression of the following forms to an off_t > 0.
1994 * 1) A positive decimal number.
1995 * 2) A positive decimal number followed by a b (mult by 512).
1996 * 3) A positive decimal number followed by a k (mult by 1024).
1997 * 4) A positive decimal number followed by a m (mult by 512).
1998 * 5) A positive decimal number followed by a w (mult by sizeof int)
1999 * 6) Two or more positive decimal numbers (with/without k,b or w).
2000 * separated by x (also * for backwards compatibility), specifying
2001 * the product of the indicated values.
2003 * 0 for an error, a positive value o.w.
2012 num
= STRTOOFFT(val
, &expr
, 0);
2013 if ((num
== OFFT_MAX
) || (num
<= 0) || (expr
== val
))
2053 num
*= str_offt(expr
+ 1);
2069 name
= fgetln(f
, &len
);
2071 get_line_error
= ferror(f
) ? GETLINE_FILE_CORRUPT
: 0;
2074 if (name
[len
-1] != '\n')
2078 get_line_error
= GETLINE_OUT_OF_MEM
;
2081 memcpy(temp
, name
, len
-1);
2088 * for those option functions where the archive format has nothing to do.
2101 * print the usage summary to the user
2108 "usage: pax [-0cdjnvzVO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
2109 " [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
2110 " [pattern ...]\n");
2112 " pax -r [-cdijknuvzADOVYZ] [-E limit] [-f archive] [-N dbdir]\n"
2113 " [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
2114 " [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
2116 " pax -w [-dijtuvzAHLMOPVX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
2117 " [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
2118 " [-U user] ... [-G group] ...\n"
2119 " [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
2121 " pax -r -w [-dijklntuvzADHLMOPVXYZ] [-N dbdir] [-p string] ...\n"
2122 " [-s replstr] ... [-U user] ... [-G group] ...\n"
2123 " [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
2130 * print the usage summary to the user
2136 (void)fputs("usage: tar [-]{crtux}[-befhjklmopqvwzHJOPSXZ014578] "
2137 "[archive] [blocksize]\n"
2138 " [-C directory] [-T file] [-s replstr] "
2139 "[file ...]\n", stderr
);
2147 * print the usage summary to the user
2154 (void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
2155 "[-H format] [-O archive]\n"
2156 " < name-list [> archive]\n"
2157 " cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
2158 "[-F archive] [-H format] \n"
2160 "[pattern ...] [< archive]\n"
2161 " cpio -p [-adlLmuv] destination-directory "
2162 "< name-list\n", stderr
);