1 /* $NetBSD: options.c,v 1.115 2013/11/14 04:00:48 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.115 2013/11/14 04:00:48 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 { "to-stdout", no_argument
, 0, 'O' },
777 { "absolute-paths", no_argument
, 0, 'P' },
778 { "sparse", no_argument
, 0, 'S' },
779 { "files-from", required_argument
, 0, 'T' },
780 { "summary", no_argument
, 0, 'V' },
781 { "stats", no_argument
, 0, 'V' },
782 { "exclude-from", required_argument
, 0, 'X' },
783 { "compress", no_argument
, 0, 'Z' },
784 { "uncompress", no_argument
, 0, 'Z' },
785 { "strict", no_argument
, 0,
787 { "atime-preserve", no_argument
, 0,
788 OPT_ATIME_PRESERVE
},
789 { "unlink", no_argument
, 0,
791 { "use-compress-program", required_argument
, 0,
792 OPT_USE_COMPRESS_PROGRAM
},
793 { "force-local", no_argument
, 0,
795 { "insecure", no_argument
, 0,
797 { "exclude", required_argument
, 0,
799 { "no-recursion", no_argument
, 0,
801 { "xz", no_argument
, 0,
803 #if !HAVE_NBTOOL_CONFIG_H
804 { "chroot", no_argument
, 0,
807 #if 0 /* Not implemented */
808 { "catenate", no_argument
, 0, 'A' }, /* F */
809 { "concatenate", no_argument
, 0, 'A' }, /* F */
810 { "diff", no_argument
, 0, 'd' }, /* F */
811 { "compare", no_argument
, 0, 'd' }, /* F */
812 { "checkpoint", no_argument
, 0,
814 { "help", no_argument
, 0,
816 { "info-script", required_argument
, 0, 'F' },
817 { "new-volume-script", required_argument
, 0, 'F' },
818 { "incremental", no_argument
, 0, 'G' },
819 { "listed-incremental", required_argument
, 0, 'g' },
820 { "ignore-zeros", no_argument
, 0, 'i' },
821 { "ignore-failed-read", no_argument
, 0,
822 OPT_IGNORE_FAILED_READ
},
823 { "starting-file", no_argument
, 0, 'K' },
824 { "tape-length", required_argument
, 0, 'L' },
825 { "multi-volume", no_argument
, 0, 'M' },
826 { "after-date", required_argument
, 0, 'N' },
827 { "newer", required_argument
, 0, 'N' },
828 { "record-number", no_argument
, 0, 'R' },
829 { "remove-files", no_argument
, 0,
831 { "same-order", no_argument
, 0, 's' },
832 { "preserve-order", no_argument
, 0, 's' },
833 { "null", no_argument
, 0,
835 { "totals", no_argument
, 0,
837 { "volume-name", required_argument
, 0, 'V' }, /* XXX */
838 { "label", required_argument
, 0, 'V' }, /* XXX */
839 { "version", no_argument
, 0,
841 { "verify", no_argument
, 0, 'W' },
842 { "block-compress", no_argument
, 0,
843 OPT_BLOCK_COMPRESS
},
849 tar_set_action(int op
)
851 if (act
!= ERROR
&& act
!= op
)
857 tar_options(int argc
, char **argv
)
863 int incfiles_max
= 0;
868 struct incfile
*incfiles
= NULL
;
871 * Set default values.
877 * process option flags
879 while ((c
= getoldopt(argc
, argv
,
880 "+b:cef:hjklmopqrs:tuvwxzBC:HI:OPST:X:Z014578",
886 * specify blocksize in 512-byte blocks
888 if ((wrblksz
= (int)str_offt(optarg
)) <= 0) {
889 tty_warn(1, "Invalid block size %s", optarg
);
892 wrblksz
*= 512; /* XXX - check for int oflow */
898 tar_set_action(ARCHIVE
);
902 * stop after first error
908 * filename where the archive is stored
910 if ((optarg
[0] == '-') && (optarg
[1]== '\0')) {
929 * pass through bzip2. not a standard option
931 gzip_program
= BZIP2_CMD
;
935 * do not clobber files that exist
941 * do not pass over mount points in the file system
947 * do not preserve modification time
953 * This option does several things based on whether
954 * this is a create or extract operation.
956 if (act
== ARCHIVE
) {
957 /* GNU tar: write V7 format archives. */
959 /* 4.2BSD: don't add directory entries. */
960 if (opt_add("write_opt=nodir") < 0)
964 /* SUS: don't preserve owner/group. */
974 * preserve user id, group id, file
975 * mode, access/modification times
985 * select first match for a pattern only
992 * append to the archive
994 tar_set_action(APPND
);
998 * file name substitution name pattern
1000 if (rep_add(optarg
) < 0) {
1007 * list contents of the tape
1009 tar_set_action(LIST
);
1013 * verbose operation mode
1019 * interactive file rename
1025 * extract an archive, preserving mode,
1026 * and mtime if possible.
1028 tar_set_action(EXTRACT
);
1033 * use gzip. Non standard option.
1035 gzip_program
= GZIP_CMD
;
1039 * Nothing to do here, this is pax default
1048 * follow command line symlinks only
1054 if (++nincfiles
> incfiles_max
) {
1055 incfiles_max
= nincfiles
+ 3;
1056 incfiles
= realloc(incfiles
,
1057 sizeof(*incfiles
) * incfiles_max
);
1058 if (incfiles
== NULL
) {
1059 tty_warn(0, "Unable to allocate space "
1064 incfiles
[nincfiles
- 1].file
= optarg
;
1065 incfiles
[nincfiles
- 1].dir
= chdname
;
1069 * do not remove leading '/' from pathnames
1075 /* do nothing; we already generate sparse files */
1079 * semi-verbose operation mode (no listing)
1085 * GNU tar compat: exclude the files listed in optarg
1087 if (tar_gnutar_X_compat(optarg
) != 0)
1094 gzip_program
= COMPRESS_CMD
;
1114 case OPT_ATIME_PRESERVE
:
1118 /* Just ignore -- we always unlink first. */
1120 case OPT_USE_COMPRESS_PROGRAM
:
1121 gzip_program
= optarg
;
1123 case OPT_FORCE_LOCAL
:
1130 /* disable gnu extensions */
1134 if (tar_gnutar_minus_minus_exclude(optarg
) != 0)
1140 #if !HAVE_NBTOOL_CONFIG_H
1146 gzip_program
= XZ_CMD
;
1156 /* Tar requires an action. */
1160 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
1161 if (fstdin
== 1 && act
== ARCHIVE
)
1166 /* Traditional tar behaviour (pax wants to read file list from stdin) */
1167 if ((act
== ARCHIVE
|| act
== APPND
) && argc
== 0 && nincfiles
== 0)
1170 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
1171 * (unless -o specified)
1173 if (act
== ARCHIVE
|| act
== APPND
)
1174 frmt
= &(fsub
[Oflag
? F_TAR
: F_USTAR
]);
1179 tty_warn(1, "The -O/-o options are only valid when "
1180 "writing or extracting an archive");
1186 * process the args as they are interpreted by the operation mode
1195 char *file
, *dir
= NULL
;
1196 int mustfreedir
= 0;
1198 while (nincfiles
|| *argv
!= NULL
) {
1200 * If we queued up any include files,
1201 * pull them in now. Otherwise, check
1202 * for -I and -C positional flags.
1203 * Anything else must be a file to
1207 file
= incfiles
->file
;
1208 dir
= incfiles
->dir
;
1212 } else if (strcmp(*argv
, "-I") == 0) {
1213 if (*++argv
== NULL
)
1227 if (strcmp(file
, "-") == 0)
1229 else if ((fp
= fopen(file
, "r")) == NULL
) {
1230 tty_warn(1, "Unable to open file '%s' for read", file
);
1233 while ((str
= get_line(fp
)) != NULL
) {
1235 if (dir
&& mustfreedir
)
1242 if (strcmp(str
, "-C") == 0) {
1248 if (strncmp(str
, "-C ", 3) == 0) {
1250 if (dir
&& mustfreedir
)
1252 dir
= strdup(str
+ 3);
1257 if (pat_add(str
, dir
, NOGLOB_MTCH
) < 0)
1261 /* Bomb if given -C w/out a dir. */
1264 if (dir
&& mustfreedir
)
1266 if (strcmp(file
, "-") != 0)
1268 if (get_line_error
) {
1269 tty_warn(1, "Problem with file '%s'", file
);
1272 } else if (strcmp(*argv
, "-C") == 0) {
1273 if (*++argv
== NULL
)
1277 } else if (pat_add(*argv
++, chdname
, 0) < 0)
1283 * if patterns were added, we are doing chdir()
1284 * on a file-by-file basis, else, just one
1285 * global chdir (if any) after opening input.
1293 if (chdname
!= NULL
) { /* initial chdir() */
1294 if (ftree_add(chdname
, 1) < 0)
1298 while (nincfiles
|| *argv
!= NULL
) {
1302 * If we queued up any include files, pull them in
1303 * now. Otherwise, check for -I and -C positional
1304 * flags. Anything else must be a file to include
1308 file
= incfiles
->file
;
1309 dir
= incfiles
->dir
;
1312 } else if (strcmp(*argv
, "-I") == 0) {
1313 if (*++argv
== NULL
)
1326 /* Set directory if needed */
1328 if (ftree_add(dir
, 1) < 0)
1332 if (strcmp(file
, "-") == 0)
1334 else if ((fp
= fopen(file
, "r")) == NULL
) {
1335 tty_warn(1, "Unable to open file '%s' for read", file
);
1338 while ((str
= get_line(fp
)) != NULL
) {
1340 if (ftree_add(str
, 1) < 0)
1345 if (strcmp(str
, "-C") == 0) {
1349 if (strncmp(str
, "-C ", 3) == 0) {
1350 if (ftree_add(str
+ 3, 1) < 0)
1354 if (ftree_add(str
, 0) < 0)
1357 /* Bomb if given -C w/out a dir. */
1360 if (strcmp(file
, "-") != 0)
1362 if (get_line_error
) {
1363 tty_warn(1, "Problem with file '%s'",
1367 } else if (strcmp(*argv
, "-C") == 0) {
1368 if (*++argv
== NULL
)
1370 if (ftree_add(*argv
++, 1) < 0)
1372 } else if (ftree_add(*argv
++, 0) < 0)
1376 * no read errors allowed on updates/append operation!
1381 if (!fstdin
&& ((arcname
== NULL
) || (*arcname
== '\0'))) {
1382 arcname
= getenv("TAPE");
1383 if ((arcname
== NULL
) || (*arcname
== '\0'))
1384 arcname
= _PATH_DEFTAPE
;
1397 slash
+= strspn(slash
, "/");
1398 slash
+= strcspn(slash
, "/");
1400 done
= (*slash
== '\0');
1403 if (domkdir(path
, 0777) == -1)
1412 /* Can't create or or not a directory */
1413 syswarn(1, errno
, "Cannot create directory `%s'", path
);
1419 struct option cpio_longopts
[] = {
1420 { "reset-access-time", no_argument
, 0, 'a' },
1421 { "make-directories", no_argument
, 0, 'd' },
1422 { "nonmatching", no_argument
, 0, 'f' },
1423 { "extract", no_argument
, 0, 'i' },
1424 { "link", no_argument
, 0, 'l' },
1425 { "preserve-modification-time", no_argument
, 0, 'm' },
1426 { "create", no_argument
, 0, 'o' },
1427 { "pass-through", no_argument
, 0, 'p' },
1428 { "rename", no_argument
, 0, 'r' },
1429 { "list", no_argument
, 0, 't' },
1430 { "unconditional", no_argument
, 0, 'u' },
1431 { "verbose", no_argument
, 0, 'v' },
1432 { "append", no_argument
, 0, 'A' },
1433 { "pattern-file", required_argument
, 0, 'E' },
1434 { "file", required_argument
, 0, 'F' },
1435 { "force-local", no_argument
, 0,
1437 { "format", required_argument
, 0, 'H' },
1438 { "dereference", no_argument
, 0, 'L' },
1439 { "swap-halfwords", no_argument
, 0, 'S' },
1440 { "summary", no_argument
, 0, 'V' },
1441 { "stats", no_argument
, 0, 'V' },
1442 { "insecure", no_argument
, 0,
1444 { "sparse", no_argument
, 0,
1446 { "xz", no_argument
, 0,
1450 /* Not implemented */
1451 { "null", no_argument
, 0, '0' },
1452 { "swap", no_argument
, 0, 'b' },
1453 { "numeric-uid-gid", no_argument
, 0, 'n' },
1454 { "swap-bytes", no_argument
, 0, 's' },
1455 { "message", required_argument
, 0, 'M' },
1456 { "owner", required_argument
, 0 'R
' },
1457 { "dot", no_argument, 0, 'V
' }, /* xxx */
1458 { "block-size", required_argument, 0,
1460 { "no-absolute-pathnames", no_argument, 0,
1461 OPT_NO_ABSOLUTE_PATHNAMES },
1462 { "no-preserve-owner", no_argument, 0,
1463 OPT_NO_PRESERVE_OWNER },
1464 { "only-verify-crc", no_argument, 0,
1465 OPT_ONLY_VERIFY_CRC },
1466 { "rsh-command", required_argument, 0,
1468 { "version", no_argument, 0,
1475 cpio_set_action(int op)
1477 if ((act == APPND && op == ARCHIVE) || (act == ARCHIVE && op == APPND))
1479 else if (act == EXTRACT && op == LIST)
1481 else if (act != ERROR && act != op)
1489 * look at the user specified flags. set globals as required and check if
1490 * the user specified a legal set of flags. If not, complain and exit
1494 cpio_options(int argc, char **argv)
1513 * process option flags
1515 while ((c = getoldopt(argc, argv,
1516 "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
1517 cpio_longopts, NULL)) != -1) {
1521 * preserve access time on filesystem nodes we read
1529 * swap bytes and half-words when reading data
1537 frmt = &fsub[F_SV4CPIO];
1541 * create directories as needed
1542 * pax does this by default ..
1548 * inverse match on patterns
1557 cpio_set_action(EXTRACT);
1566 * try to link src to dest with copy (-rw)
1582 cpio_set_action(ARCHIVE);
1583 frmt = &(fsub[F_SV4CRC]);
1588 * cpio -p is like pax -rw
1590 cpio_set_action(COPY);
1595 * interactive file rename
1603 * swap bytes after reading data
1609 * list contents of archive
1611 cpio_set_action(LIST);
1617 * don't ignore those older files
1625 * verbose operation mode
1632 * use gzip. Non standard option.
1634 gzip_program
= GZIP_CMD
;
1638 * append to an archive
1640 cpio_set_action(APPND
);
1645 * set blocksize to 5120
1653 if ((blksz
= (int)str_offt(optarg
)) <= 0) {
1654 tty_warn(1, "Invalid block size %s", optarg
);
1660 * file with patterns to extract or list
1662 if ((fp
= fopen(optarg
, "r")) == NULL
) {
1663 tty_warn(1, "Unable to open file '%s' for read",
1667 while ((str
= get_line(fp
)) != NULL
) {
1668 pat_add(str
, NULL
, 0);
1671 if (get_line_error
) {
1672 tty_warn(1, "Problem with file '%s'", optarg
);
1678 * specify an archive format on write
1681 frmt
= (FSUB
*)bsearch((void *)&tmp
, (void *)fsub
,
1682 sizeof(fsub
)/sizeof(FSUB
), sizeof(FSUB
), c_frmt
);
1687 tty_warn(1, "Unknown -H format: %s", optarg
);
1688 (void)fputs("cpio: Known -H formats are:", stderr
);
1689 for (i
= 0; i
< (sizeof(fsub
)/sizeof(FSUB
)); ++i
)
1690 (void)fprintf(stderr
, " %s", fsub
[i
].name
);
1691 (void)fputs("\n\n", stderr
);
1698 * filename where the archive is stored
1700 if ((optarg
[0] == '-') && (optarg
[1]== '\0')) {
1702 * treat a - as stdin
1726 * swap halfwords after reading data
1731 case 'V': /* print a '.' for each file processed */
1736 * semi-verbose operation mode (no listing)
1743 * use compress. Non standard option.
1745 gzip_program
= COMPRESS_CMD
;
1749 * process Version 6 cpio format
1751 frmt
= &(fsub
[F_BCPIO
]);
1753 case OPT_FORCE_LOCAL
:
1760 /* do nothing; we already generate sparse files */
1763 gzip_program
= XZ_CMD
;
1772 * figure out the operation mode of cpio. check that we have not been
1773 * given a bogus set of flags for the operation mode.
1777 bflg
= flg
& BDLIST
;
1778 } else if (ISEXTRACT(flg
)) {
1780 bflg
= flg
& BDEXTR
;
1781 } else if (ISARCHIVE(flg
)) {
1783 bflg
= flg
& BDARCH
;
1784 } else if (ISAPPND(flg
)) {
1786 bflg
= flg
& BDARCH
;
1787 } else if (ISCOPY(flg
)) {
1789 bflg
= flg
& BDCOPY
;
1797 * if we are writing (ARCHIVE) we use the default format if the user
1798 * did not specify a format. when we write during an APPEND, we will
1799 * adopt the format of the existing archive if none was supplied.
1801 if (!(flg
& XF
) && (act
== ARCHIVE
))
1802 frmt
= &(fsub
[F_BCPIO
]);
1805 * process the args as they are interpreted by the operation mode
1810 for (; optind
< argc
; optind
++)
1811 if (pat_add(argv
[optind
], NULL
, 0) < 0)
1815 if (optind
>= argc
) {
1816 tty_warn(0, "Destination directory was not supplied");
1820 dirptr
= argv
[argc
];
1824 if (argc
!= optind
) {
1825 for (; optind
< argc
; optind
++)
1826 if (ftree_add(argv
[optind
], 0) < 0)
1831 * no read errors allowed on updates/append operation!
1834 while ((str
= get_line(stdin
)) != NULL
) {
1837 if (get_line_error
) {
1838 tty_warn(1, "Problem while reading stdin");
1851 * print out those invalid flag sets found to the user
1855 printflg(unsigned int flg
)
1859 (void)fprintf(stderr
,"%s: Invalid combination of options:", argv0
);
1860 while ((nxt
= ffs(flg
)) != 0) {
1861 flg
&= ~(1 << (nxt
- 1));
1862 (void)fprintf(stderr
, " -%c", flgch
[nxt
- 1]);
1864 (void)putc('\n', stderr
);
1869 * comparison routine used by bsearch to find the format specified
1874 c_frmt(const void *a
, const void *b
)
1876 return strcmp(((const FSUB
*)a
)->name
, ((const FSUB
*)b
)->name
);
1881 * called by format specific options routines to get each format specific
1882 * flag and value specified with -o
1884 * pointer to next OPLIST entry or NULL (end of list).
1892 if ((opt
= ophead
) != NULL
)
1893 ophead
= ophead
->fow
;
1899 * generic routine used to complain about a format specific options
1900 * when the format does not support options.
1911 * print all we were given
1913 tty_warn(1," These format options are not supported for %s",
1915 while ((opt
= opt_next()) != NULL
)
1916 (void)fprintf(stderr
, "\t%s = %s\n", opt
->name
, opt
->value
);
1917 if (strcmp(NM_TAR
, argv0
) == 0)
1920 else if (strcmp(NM_CPIO
, argv0
) == 0)
1930 * breaks the value supplied to -o into a option name and value. options
1931 * are given to -o in the form -o name-value,name=value
1932 * multiple -o may be specified.
1934 * 0 if format in name=value format, -1 if -o is passed junk
1938 opt_add(const char *str
)
1946 if ((str
== NULL
) || (*str
== '\0')) {
1947 tty_warn(0, "Invalid option name");
1950 if ((dstr
= strdup(str
)) == NULL
) {
1951 tty_warn(0, "Unable to allocate space for option list");
1954 frpt
= endpt
= dstr
;
1957 * break into name and values pieces and stuff each one into a
1958 * OPLIST structure. When we know the format, the format specific
1959 * option function will go through this list
1961 while ((frpt
!= NULL
) && (*frpt
!= '\0')) {
1962 if ((endpt
= strchr(frpt
, ',')) != NULL
)
1964 if ((pt
= strchr(frpt
, '=')) == NULL
) {
1965 tty_warn(0, "Invalid options format");
1969 if ((opt
= (OPLIST
*)malloc(sizeof(OPLIST
))) == NULL
) {
1970 tty_warn(0, "Unable to allocate space for option list");
1982 if (ophead
== NULL
) {
1983 optail
= ophead
= opt
;
1994 * Convert an expression of the following forms to an off_t > 0.
1995 * 1) A positive decimal number.
1996 * 2) A positive decimal number followed by a b (mult by 512).
1997 * 3) A positive decimal number followed by a k (mult by 1024).
1998 * 4) A positive decimal number followed by a m (mult by 512).
1999 * 5) A positive decimal number followed by a w (mult by sizeof int)
2000 * 6) Two or more positive decimal numbers (with/without k,b or w).
2001 * separated by x (also * for backwards compatibility), specifying
2002 * the product of the indicated values.
2004 * 0 for an error, a positive value o.w.
2013 num
= STRTOOFFT(val
, &expr
, 0);
2014 if ((num
== OFFT_MAX
) || (num
<= 0) || (expr
== val
))
2054 num
*= str_offt(expr
+ 1);
2070 name
= fgetln(f
, &len
);
2072 get_line_error
= ferror(f
) ? GETLINE_FILE_CORRUPT
: 0;
2075 if (name
[len
-1] != '\n')
2079 get_line_error
= GETLINE_OUT_OF_MEM
;
2082 memcpy(temp
, name
, len
-1);
2089 * for those option functions where the archive format has nothing to do.
2102 * print the usage summary to the user
2109 "usage: pax [-0cdjnvzVO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
2110 " [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
2111 " [pattern ...]\n");
2113 " pax -r [-cdijknuvzADOVYZ] [-E limit] [-f archive] [-N dbdir]\n"
2114 " [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
2115 " [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
2117 " pax -w [-dijtuvzAHLMOPVX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
2118 " [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
2119 " [-U user] ... [-G group] ...\n"
2120 " [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
2122 " pax -r -w [-dijklntuvzADHLMOPVXYZ] [-N dbdir] [-p string] ...\n"
2123 " [-s replstr] ... [-U user] ... [-G group] ...\n"
2124 " [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
2131 * print the usage summary to the user
2137 (void)fputs("usage: tar [-]{crtux}[-befhjklmopqvwzHOPSXZ014578] [archive] "
2139 " [-C directory] [-T file] [-s replstr] "
2140 "[file ...]\n", stderr
);
2148 * print the usage summary to the user
2155 (void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
2156 "[-H format] [-O archive]\n"
2157 " < name-list [> archive]\n"
2158 " cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
2159 "[-F archive] [-H format] \n"
2161 "[pattern ...] [< archive]\n"
2162 " cpio -p [-adlLmuv] destination-directory "
2163 "< name-list\n", stderr
);