1 /* $NetBSD: options.c,v 1.114 2012/08/09 11:05:59 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.114 2012/08/09 11:05:59 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
131 #if !HAVE_NBTOOL_CONFIG_H
132 #define OPT_CHROOT 18
136 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
137 * (see pax.h for description of each function)
139 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
140 * read, end_read, st_write, write, end_write, trail,
141 * subtrail, rd_data, wr_data, options
146 /* 0: OLD BINARY CPIO */
147 { "bcpio", 5120, sizeof(HD_BCPIO
), 1, 0, 0, 1, bcpio_id
, cpio_strd
,
148 bcpio_rd
, bcpio_endrd
, cpio_stwr
, bcpio_wr
, cpio_endwr
, NULL
,
149 cpio_subtrail
, rd_wrfile
, wr_rdfile
, bad_opt
},
151 /* 1: OLD OCTAL CHARACTER CPIO */
152 { "cpio", 5120, sizeof(HD_CPIO
), 1, 0, 0, 1, cpio_id
, cpio_strd
,
153 cpio_rd
, cpio_endrd
, cpio_stwr
, cpio_wr
, cpio_endwr
, NULL
,
154 cpio_subtrail
, rd_wrfile
, wr_rdfile
, bad_opt
},
156 /* 2: SVR4 HEX CPIO */
157 { "sv4cpio", 5120, sizeof(HD_VCPIO
), 1, 0, 0, 1, vcpio_id
, cpio_strd
,
158 vcpio_rd
, vcpio_endrd
, cpio_stwr
, vcpio_wr
, cpio_endwr
, NULL
,
159 cpio_subtrail
, rd_wrfile
, wr_rdfile
, bad_opt
},
161 /* 3: SVR4 HEX CPIO WITH CRC */
162 { "sv4crc", 5120, sizeof(HD_VCPIO
), 1, 0, 0, 1, crc_id
, crc_strd
,
163 vcpio_rd
, vcpio_endrd
, crc_stwr
, vcpio_wr
, cpio_endwr
, NULL
,
164 cpio_subtrail
, rd_wrfile
, wr_rdfile
, bad_opt
},
167 { "tar", 10240, BLKMULT
, 0, 1, BLKMULT
, 0, tar_id
, no_op
,
168 tar_rd
, tar_endrd
, no_op
, tar_wr
, tar_endwr
, tar_trail
,
169 NULL
, rd_wrfile
, wr_rdfile
, tar_opt
},
172 { "ustar", 10240, BLKMULT
, 0, 1, BLKMULT
, 0, ustar_id
, ustar_strd
,
173 ustar_rd
, tar_endrd
, ustar_stwr
, ustar_wr
, tar_endwr
, tar_trail
,
174 NULL
, rd_wrfile
, wr_rdfile
, bad_opt
}
177 #define F_BCPIO 0 /* old binary cpio format */
178 #define F_CPIO 1 /* old octal character cpio format */
179 #define F_SV4CPIO 2 /* SVR4 hex cpio format */
180 #define F_SV4CRC 3 /* SVR4 hex with crc cpio format */
181 #define F_TAR 4 /* old V7 UNIX tar format */
182 #define F_USTAR 5 /* ustar format */
184 #define F_TAR 0 /* old V7 UNIX tar format */
185 #define F_USTAR 1 /* ustar format */
187 #define DEFLT F_USTAR /* default write format from list above */
190 * ford is the archive search order used by get_arc() to determine what kind
191 * of archive we are dealing with. This helps to properly id archive formats
192 * some formats may be subsets of others....
194 int ford
[] = {F_USTAR
, F_TAR
,
196 F_SV4CRC
, F_SV4CPIO
, F_CPIO
, F_BCPIO
,
201 * filename record separator
206 * Do we have -C anywhere?
212 * figure out if we are pax, tar or cpio. Call the appropriate options
217 options(int argc
, char **argv
)
221 * Are we acting like pax, tar or cpio (based on argv[0])
223 if ((argv0
= strrchr(argv
[0], '/')) != NULL
)
228 if (strstr(argv0
, NM_TAR
)) {
230 tar_options(argc
, argv
);
232 } else if (strstr(argv0
, NM_CPIO
)) {
234 cpio_options(argc
, argv
);
238 pax_options(argc
, argv
);
242 struct option pax_longopts
[] = {
243 { "insecure", no_argument
, 0,
245 { "force-local", no_argument
, 0,
247 { "use-compress-program", required_argument
, 0,
248 OPT_USE_COMPRESS_PROGRAM
},
249 { "xz", no_argument
, 0,
257 * look at the user specified flags. set globals as required and check if
258 * the user specified a legal set of flags. If not, complain and exit
262 pax_options(int argc
, char **argv
)
272 * process option flags
274 while ((c
= getopt_long(argc
, argv
,
275 "0ab:cdf:ijklno:p:rs:tuvwx:zAB:DE:G:HLMN:OPT:U:VXYZ",
276 pax_longopts
, NULL
)) != -1) {
292 if ((wrblksz
= (int)str_offt(optarg
)) <= 0) {
293 tty_warn(1, "Invalid block size %s", optarg
);
299 * inverse match on patterns
306 * match only dir on extract, not the subtree at dir
313 * filename where the archive is stored
320 * interactive file rename
329 gzip_program
= BZIP2_CMD
;
333 * do not clobber files that exist
340 * try to link src to dest with copy (-rw)
347 * select first match for a pattern only
354 * pass format specific options
357 if (opt_add(optarg
) < 0)
362 * specify file characteristic options
364 for (pt
= optarg
; *pt
!= '\0'; ++pt
) {
368 * do not preserve access time
374 * preserve user id, group id, file
375 * mode, access/modification times
387 * do not preserve file flags
394 * do not preserve modification time
406 * preserve file mode bits
411 tty_warn(1, "Invalid -p string: %c",
427 * file name substitution name pattern
429 if (rep_add(optarg
) < 0) {
437 * preserve access time on filesystem nodes we read
444 * ignore those older files
451 * verbose operation mode
464 * specify an archive format on write
467 frmt
= (FSUB
*)bsearch((void *)&tmp
, (void *)fsub
,
468 sizeof(fsub
)/sizeof(FSUB
), sizeof(FSUB
), c_frmt
);
473 tty_warn(1, "Unknown -x format: %s", optarg
);
474 (void)fputs("pax: Known -x formats are:", stderr
);
475 for (i
= 0; i
< (sizeof(fsub
)/sizeof(FSUB
)); ++i
)
476 (void)fprintf(stderr
, " %s", fsub
[i
].name
);
477 (void)fputs("\n\n", stderr
);
482 * use gzip. Non standard option.
484 gzip_program
= GZIP_CMD
;
492 * non-standard option on number of bytes written on a
493 * single archive volume.
495 if ((wrlimit
= str_offt(optarg
)) <= 0) {
496 tty_warn(1, "Invalid write limit %s", optarg
);
499 if (wrlimit
% BLKMULT
) {
501 "Write limit is not a %d byte multiple",
509 * On extraction check file inode change time before the
510 * modification of the file name. Non standard option.
517 * non-standard limit on read faults
518 * 0 indicates stop after first error, values
519 * indicate a limit, "none" try forever
522 if (strcmp(none
, optarg
) == 0)
524 else if ((maxflt
= atoi(optarg
)) < 0) {
526 "Error count value must be positive");
532 * non-standard option for selecting files within an
533 * archive by group (gid or name)
535 if (grp_add(optarg
) < 0) {
543 * follow command line symlinks only
558 tty_warn(1, "Support for -%c is not compiled in", c
);
563 * Treat list of filenames on stdin as an
564 * mtree(8) specfile. Non standard option.
571 * Use alternative directory for user db lookups.
573 if (!setup_getid(optarg
)) {
575 "Unable to use user and group databases in `%s'",
583 * Force one volume. Non standard option.
585 force_one_volume
= 1;
589 * do NOT follow symlinks (default)
596 * non-standard option for selecting files within an
597 * archive by modification time range (lower,upper)
599 if (trng_add(optarg
) < 0) {
607 * non-standard option for selecting files within an
608 * archive by user (uid or name)
610 if (usr_add(optarg
) < 0) {
618 * somewhat verbose operation mode (no listing)
625 * do not pass over mount points in the file system
632 * On extraction check file inode change time after the
633 * modification of the file name. Non standard option.
640 * On extraction check modification time after the
641 * modification of the file name. Non standard option.
649 case OPT_FORCE_LOCAL
:
652 case OPT_USE_COMPRESS_PROGRAM
:
653 gzip_program
= optarg
;
656 gzip_program
= XZ_CMD
;
666 * figure out the operation mode of pax read,write,extract,copy,append
667 * or list. check that we have not been given a bogus set of flags
668 * for the operation mode.
674 } else if (ISEXTRACT(flg
)) {
677 } else if (ISARCHIVE(flg
)) {
680 } else if (ISAPPND(flg
)) {
683 } else if (ISCOPY(flg
)) {
694 * if we are writing (ARCHIVE) we use the default format if the user
695 * did not specify a format. when we write during an APPEND, we will
696 * adopt the format of the existing archive if none was supplied.
698 if (!(flg
& XF
) && (act
== ARCHIVE
))
699 frmt
= &(fsub
[DEFLT
]);
702 * process the args as they are interpreted by the operation mode
707 for (; optind
< argc
; optind
++)
708 if (pat_add(argv
[optind
], NULL
, 0) < 0)
712 if (optind
>= argc
) {
713 tty_warn(0, "Destination directory was not supplied");
718 if (mkpath(dirptr
) < 0)
723 for (; optind
< argc
; optind
++)
724 if (ftree_add(argv
[optind
], 0) < 0)
727 * no read errors allowed on updates/append operation!
737 * look at the user specified flags. set globals as required and check if
738 * the user specified a legal set of flags. If not, complain and exit
741 struct option tar_longopts
[] = {
742 { "block-size", required_argument
, 0, 'b' },
743 { "bunzip2", no_argument
, 0, 'j' },
744 { "bzip2", no_argument
, 0, 'j' },
745 { "create", no_argument
, 0, 'c' }, /* F */
746 /* -e -- no corresponding long option */
747 { "file", required_argument
, 0, 'f' },
748 { "dereference", no_argument
, 0, 'h' },
749 { "keep-old-files", no_argument
, 0, 'k' },
750 { "one-file-system", no_argument
, 0, 'l' },
751 { "modification-time", no_argument
, 0, 'm' },
752 { "old-archive", no_argument
, 0, 'o' },
753 { "portability", no_argument
, 0, 'o' },
754 { "same-permissions", no_argument
, 0, 'p' },
755 { "preserve-permissions", no_argument
, 0, 'p' },
756 { "preserve", no_argument
, 0, 'p' },
757 { "fast-read", no_argument
, 0, 'q' },
758 { "append", no_argument
, 0, 'r' }, /* F */
759 { "update", no_argument
, 0, 'u' }, /* F */
760 { "list", no_argument
, 0, 't' }, /* F */
761 { "verbose", no_argument
, 0, 'v' },
762 { "interactive", no_argument
, 0, 'w' },
763 { "confirmation", no_argument
, 0, 'w' },
764 { "extract", no_argument
, 0, 'x' }, /* F */
765 { "get", no_argument
, 0, 'x' }, /* F */
766 { "gzip", no_argument
, 0, 'z' },
767 { "gunzip", no_argument
, 0, 'z' },
768 { "read-full-blocks", no_argument
, 0, 'B' },
769 { "directory", required_argument
, 0, 'C' },
770 { "to-stdout", no_argument
, 0, 'O' },
771 { "absolute-paths", no_argument
, 0, 'P' },
772 { "sparse", no_argument
, 0, 'S' },
773 { "files-from", required_argument
, 0, 'T' },
774 { "summary", no_argument
, 0, 'V' },
775 { "stats", no_argument
, 0, 'V' },
776 { "exclude-from", required_argument
, 0, 'X' },
777 { "compress", no_argument
, 0, 'Z' },
778 { "uncompress", no_argument
, 0, 'Z' },
779 { "strict", no_argument
, 0,
781 { "atime-preserve", no_argument
, 0,
782 OPT_ATIME_PRESERVE
},
783 { "unlink", no_argument
, 0,
785 { "use-compress-program", required_argument
, 0,
786 OPT_USE_COMPRESS_PROGRAM
},
787 { "force-local", no_argument
, 0,
789 { "insecure", no_argument
, 0,
791 { "exclude", required_argument
, 0,
793 { "no-recursion", no_argument
, 0,
795 { "xz", no_argument
, 0,
797 #if !HAVE_NBTOOL_CONFIG_H
798 { "chroot", no_argument
, 0,
801 #if 0 /* Not implemented */
802 { "catenate", no_argument
, 0, 'A' }, /* F */
803 { "concatenate", no_argument
, 0, 'A' }, /* F */
804 { "diff", no_argument
, 0, 'd' }, /* F */
805 { "compare", no_argument
, 0, 'd' }, /* F */
806 { "checkpoint", no_argument
, 0,
808 { "help", no_argument
, 0,
810 { "info-script", required_argument
, 0, 'F' },
811 { "new-volume-script", required_argument
, 0, 'F' },
812 { "incremental", no_argument
, 0, 'G' },
813 { "listed-incremental", required_argument
, 0, 'g' },
814 { "ignore-zeros", no_argument
, 0, 'i' },
815 { "ignore-failed-read", no_argument
, 0,
816 OPT_IGNORE_FAILED_READ
},
817 { "starting-file", no_argument
, 0, 'K' },
818 { "tape-length", required_argument
, 0, 'L' },
819 { "multi-volume", no_argument
, 0, 'M' },
820 { "after-date", required_argument
, 0, 'N' },
821 { "newer", required_argument
, 0, 'N' },
822 { "record-number", no_argument
, 0, 'R' },
823 { "remove-files", no_argument
, 0,
825 { "same-order", no_argument
, 0, 's' },
826 { "preserve-order", no_argument
, 0, 's' },
827 { "null", no_argument
, 0,
829 { "totals", no_argument
, 0,
831 { "volume-name", required_argument
, 0, 'V' }, /* XXX */
832 { "label", required_argument
, 0, 'V' }, /* XXX */
833 { "version", no_argument
, 0,
835 { "verify", no_argument
, 0, 'W' },
836 { "block-compress", no_argument
, 0,
837 OPT_BLOCK_COMPRESS
},
843 tar_set_action(int op
)
845 if (act
!= ERROR
&& act
!= op
)
851 tar_options(int argc
, char **argv
)
857 int incfiles_max
= 0;
862 struct incfile
*incfiles
= NULL
;
865 * Set default values.
871 * process option flags
873 while ((c
= getoldopt(argc
, argv
,
874 "+b:cef:hjklmopqrs:tuvwxzBC:HI:OPST:X:Z014578",
880 * specify blocksize in 512-byte blocks
882 if ((wrblksz
= (int)str_offt(optarg
)) <= 0) {
883 tty_warn(1, "Invalid block size %s", optarg
);
886 wrblksz
*= 512; /* XXX - check for int oflow */
892 tar_set_action(ARCHIVE
);
896 * stop after first error
902 * filename where the archive is stored
904 if ((optarg
[0] == '-') && (optarg
[1]== '\0')) {
923 * pass through bzip2. not a standard option
925 gzip_program
= BZIP2_CMD
;
929 * do not clobber files that exist
935 * do not pass over mount points in the file system
941 * do not preserve modification time
947 * This option does several things based on whether
948 * this is a create or extract operation.
950 if (act
== ARCHIVE
) {
951 /* GNU tar: write V7 format archives. */
953 /* 4.2BSD: don't add directory entries. */
954 if (opt_add("write_opt=nodir") < 0)
958 /* SUS: don't preserve owner/group. */
968 * preserve user id, group id, file
969 * mode, access/modification times
979 * select first match for a pattern only
986 * append to the archive
988 tar_set_action(APPND
);
992 * file name substitution name pattern
994 if (rep_add(optarg
) < 0) {
1001 * list contents of the tape
1003 tar_set_action(LIST
);
1007 * verbose operation mode
1013 * interactive file rename
1019 * extract an archive, preserving mode,
1020 * and mtime if possible.
1022 tar_set_action(EXTRACT
);
1027 * use gzip. Non standard option.
1029 gzip_program
= GZIP_CMD
;
1033 * Nothing to do here, this is pax default
1042 * follow command line symlinks only
1048 if (++nincfiles
> incfiles_max
) {
1049 incfiles_max
= nincfiles
+ 3;
1050 incfiles
= realloc(incfiles
,
1051 sizeof(*incfiles
) * incfiles_max
);
1052 if (incfiles
== NULL
) {
1053 tty_warn(0, "Unable to allocate space "
1058 incfiles
[nincfiles
- 1].file
= optarg
;
1059 incfiles
[nincfiles
- 1].dir
= chdname
;
1063 * do not remove leading '/' from pathnames
1069 /* do nothing; we already generate sparse files */
1073 * semi-verbose operation mode (no listing)
1079 * GNU tar compat: exclude the files listed in optarg
1081 if (tar_gnutar_X_compat(optarg
) != 0)
1088 gzip_program
= COMPRESS_CMD
;
1108 case OPT_ATIME_PRESERVE
:
1112 /* Just ignore -- we always unlink first. */
1114 case OPT_USE_COMPRESS_PROGRAM
:
1115 gzip_program
= optarg
;
1117 case OPT_FORCE_LOCAL
:
1124 /* disable gnu extensions */
1128 if (tar_gnutar_minus_minus_exclude(optarg
) != 0)
1134 #if !HAVE_NBTOOL_CONFIG_H
1140 gzip_program
= XZ_CMD
;
1150 /* Tar requires an action. */
1154 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
1155 if (fstdin
== 1 && act
== ARCHIVE
)
1160 /* Traditional tar behaviour (pax wants to read file list from stdin) */
1161 if ((act
== ARCHIVE
|| act
== APPND
) && argc
== 0 && nincfiles
== 0)
1164 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
1165 * (unless -o specified)
1167 if (act
== ARCHIVE
|| act
== APPND
)
1168 frmt
= &(fsub
[Oflag
? F_TAR
: F_USTAR
]);
1173 tty_warn(1, "The -O/-o options are only valid when "
1174 "writing or extracting an archive");
1180 * process the args as they are interpreted by the operation mode
1189 char *file
, *dir
= NULL
;
1190 int mustfreedir
= 0;
1192 while (nincfiles
|| *argv
!= NULL
) {
1194 * If we queued up any include files,
1195 * pull them in now. Otherwise, check
1196 * for -I and -C positional flags.
1197 * Anything else must be a file to
1201 file
= incfiles
->file
;
1202 dir
= incfiles
->dir
;
1206 } else if (strcmp(*argv
, "-I") == 0) {
1207 if (*++argv
== NULL
)
1221 if (strcmp(file
, "-") == 0)
1223 else if ((fp
= fopen(file
, "r")) == NULL
) {
1224 tty_warn(1, "Unable to open file '%s' for read", file
);
1227 while ((str
= get_line(fp
)) != NULL
) {
1229 if (dir
&& mustfreedir
)
1236 if (strcmp(str
, "-C") == 0) {
1242 if (strncmp(str
, "-C ", 3) == 0) {
1244 if (dir
&& mustfreedir
)
1246 dir
= strdup(str
+ 3);
1251 if (pat_add(str
, dir
, NOGLOB_MTCH
) < 0)
1255 /* Bomb if given -C w/out a dir. */
1258 if (dir
&& mustfreedir
)
1260 if (strcmp(file
, "-") != 0)
1262 if (get_line_error
) {
1263 tty_warn(1, "Problem with file '%s'", file
);
1266 } else if (strcmp(*argv
, "-C") == 0) {
1267 if (*++argv
== NULL
)
1271 } else if (pat_add(*argv
++, chdname
, 0) < 0)
1277 * if patterns were added, we are doing chdir()
1278 * on a file-by-file basis, else, just one
1279 * global chdir (if any) after opening input.
1287 if (chdname
!= NULL
) { /* initial chdir() */
1288 if (ftree_add(chdname
, 1) < 0)
1292 while (nincfiles
|| *argv
!= NULL
) {
1296 * If we queued up any include files, pull them in
1297 * now. Otherwise, check for -I and -C positional
1298 * flags. Anything else must be a file to include
1302 file
= incfiles
->file
;
1303 dir
= incfiles
->dir
;
1306 } else if (strcmp(*argv
, "-I") == 0) {
1307 if (*++argv
== NULL
)
1320 /* Set directory if needed */
1322 if (ftree_add(dir
, 1) < 0)
1326 if (strcmp(file
, "-") == 0)
1328 else if ((fp
= fopen(file
, "r")) == NULL
) {
1329 tty_warn(1, "Unable to open file '%s' for read", file
);
1332 while ((str
= get_line(fp
)) != NULL
) {
1334 if (ftree_add(str
, 1) < 0)
1339 if (strcmp(str
, "-C") == 0) {
1343 if (strncmp(str
, "-C ", 3) == 0) {
1344 if (ftree_add(str
+ 3, 1) < 0)
1348 if (ftree_add(str
, 0) < 0)
1351 /* Bomb if given -C w/out a dir. */
1354 if (strcmp(file
, "-") != 0)
1356 if (get_line_error
) {
1357 tty_warn(1, "Problem with file '%s'",
1361 } else if (strcmp(*argv
, "-C") == 0) {
1362 if (*++argv
== NULL
)
1364 if (ftree_add(*argv
++, 1) < 0)
1366 } else if (ftree_add(*argv
++, 0) < 0)
1370 * no read errors allowed on updates/append operation!
1375 if (!fstdin
&& ((arcname
== NULL
) || (*arcname
== '\0'))) {
1376 arcname
= getenv("TAPE");
1377 if ((arcname
== NULL
) || (*arcname
== '\0'))
1378 arcname
= _PATH_DEFTAPE
;
1391 slash
+= strspn(slash
, "/");
1392 slash
+= strcspn(slash
, "/");
1394 done
= (*slash
== '\0');
1397 if (domkdir(path
, 0777) == -1)
1406 /* Can't create or or not a directory */
1407 syswarn(1, errno
, "Cannot create directory `%s'", path
);
1413 struct option cpio_longopts
[] = {
1414 { "reset-access-time", no_argument
, 0, 'a' },
1415 { "make-directories", no_argument
, 0, 'd' },
1416 { "nonmatching", no_argument
, 0, 'f' },
1417 { "extract", no_argument
, 0, 'i' },
1418 { "link", no_argument
, 0, 'l' },
1419 { "preserve-modification-time", no_argument
, 0, 'm' },
1420 { "create", no_argument
, 0, 'o' },
1421 { "pass-through", no_argument
, 0, 'p' },
1422 { "rename", no_argument
, 0, 'r' },
1423 { "list", no_argument
, 0, 't' },
1424 { "unconditional", no_argument
, 0, 'u' },
1425 { "verbose", no_argument
, 0, 'v' },
1426 { "append", no_argument
, 0, 'A' },
1427 { "pattern-file", required_argument
, 0, 'E' },
1428 { "file", required_argument
, 0, 'F' },
1429 { "force-local", no_argument
, 0,
1431 { "format", required_argument
, 0, 'H' },
1432 { "dereference", no_argument
, 0, 'L' },
1433 { "swap-halfwords", no_argument
, 0, 'S' },
1434 { "summary", no_argument
, 0, 'V' },
1435 { "stats", no_argument
, 0, 'V' },
1436 { "insecure", no_argument
, 0,
1438 { "sparse", no_argument
, 0,
1440 { "xz", no_argument
, 0,
1444 /* Not implemented */
1445 { "null", no_argument
, 0, '0' },
1446 { "swap", no_argument
, 0, 'b' },
1447 { "numeric-uid-gid", no_argument
, 0, 'n' },
1448 { "swap-bytes", no_argument
, 0, 's' },
1449 { "message", required_argument
, 0, 'M' },
1450 { "owner", required_argument
, 0 'R
' },
1451 { "dot", no_argument, 0, 'V
' }, /* xxx */
1452 { "block-size", required_argument, 0,
1454 { "no-absolute-pathnames", no_argument, 0,
1455 OPT_NO_ABSOLUTE_PATHNAMES },
1456 { "no-preserve-owner", no_argument, 0,
1457 OPT_NO_PRESERVE_OWNER },
1458 { "only-verify-crc", no_argument, 0,
1459 OPT_ONLY_VERIFY_CRC },
1460 { "rsh-command", required_argument, 0,
1462 { "version", no_argument, 0,
1469 cpio_set_action(int op)
1471 if ((act == APPND && op == ARCHIVE) || (act == ARCHIVE && op == APPND))
1473 else if (act == EXTRACT && op == LIST)
1475 else if (act != ERROR && act != op)
1483 * look at the user specified flags. set globals as required and check if
1484 * the user specified a legal set of flags. If not, complain and exit
1488 cpio_options(int argc, char **argv)
1507 * process option flags
1509 while ((c = getoldopt(argc, argv,
1510 "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
1511 cpio_longopts, NULL)) != -1) {
1515 * preserve access time on filesystem nodes we read
1523 * swap bytes and half-words when reading data
1531 frmt = &fsub[F_SV4CPIO];
1535 * create directories as needed
1536 * pax does this by default ..
1542 * inverse match on patterns
1551 cpio_set_action(EXTRACT);
1560 * try to link src to dest with copy (-rw)
1576 cpio_set_action(ARCHIVE);
1577 frmt = &(fsub[F_SV4CRC]);
1582 * cpio -p is like pax -rw
1584 cpio_set_action(COPY);
1589 * interactive file rename
1597 * swap bytes after reading data
1603 * list contents of archive
1605 cpio_set_action(LIST);
1611 * don't ignore those older files
1619 * verbose operation mode
1626 * use gzip. Non standard option.
1628 gzip_program
= GZIP_CMD
;
1632 * append to an archive
1634 cpio_set_action(APPND
);
1639 * set blocksize to 5120
1647 if ((blksz
= (int)str_offt(optarg
)) <= 0) {
1648 tty_warn(1, "Invalid block size %s", optarg
);
1654 * file with patterns to extract or list
1656 if ((fp
= fopen(optarg
, "r")) == NULL
) {
1657 tty_warn(1, "Unable to open file '%s' for read",
1661 while ((str
= get_line(fp
)) != NULL
) {
1662 pat_add(str
, NULL
, 0);
1665 if (get_line_error
) {
1666 tty_warn(1, "Problem with file '%s'", optarg
);
1672 * specify an archive format on write
1675 frmt
= (FSUB
*)bsearch((void *)&tmp
, (void *)fsub
,
1676 sizeof(fsub
)/sizeof(FSUB
), sizeof(FSUB
), c_frmt
);
1681 tty_warn(1, "Unknown -H format: %s", optarg
);
1682 (void)fputs("cpio: Known -H formats are:", stderr
);
1683 for (i
= 0; i
< (sizeof(fsub
)/sizeof(FSUB
)); ++i
)
1684 (void)fprintf(stderr
, " %s", fsub
[i
].name
);
1685 (void)fputs("\n\n", stderr
);
1692 * filename where the archive is stored
1694 if ((optarg
[0] == '-') && (optarg
[1]== '\0')) {
1696 * treat a - as stdin
1720 * swap halfwords after reading data
1725 case 'V': /* print a '.' for each file processed */
1730 * semi-verbose operation mode (no listing)
1737 * use compress. Non standard option.
1739 gzip_program
= COMPRESS_CMD
;
1743 * process Version 6 cpio format
1745 frmt
= &(fsub
[F_BCPIO
]);
1747 case OPT_FORCE_LOCAL
:
1754 /* do nothing; we already generate sparse files */
1757 gzip_program
= XZ_CMD
;
1766 * figure out the operation mode of cpio. check that we have not been
1767 * given a bogus set of flags for the operation mode.
1771 bflg
= flg
& BDLIST
;
1772 } else if (ISEXTRACT(flg
)) {
1774 bflg
= flg
& BDEXTR
;
1775 } else if (ISARCHIVE(flg
)) {
1777 bflg
= flg
& BDARCH
;
1778 } else if (ISAPPND(flg
)) {
1780 bflg
= flg
& BDARCH
;
1781 } else if (ISCOPY(flg
)) {
1783 bflg
= flg
& BDCOPY
;
1791 * if we are writing (ARCHIVE) we use the default format if the user
1792 * did not specify a format. when we write during an APPEND, we will
1793 * adopt the format of the existing archive if none was supplied.
1795 if (!(flg
& XF
) && (act
== ARCHIVE
))
1796 frmt
= &(fsub
[F_BCPIO
]);
1799 * process the args as they are interpreted by the operation mode
1804 for (; optind
< argc
; optind
++)
1805 if (pat_add(argv
[optind
], NULL
, 0) < 0)
1809 if (optind
>= argc
) {
1810 tty_warn(0, "Destination directory was not supplied");
1814 dirptr
= argv
[argc
];
1818 if (argc
!= optind
) {
1819 for (; optind
< argc
; optind
++)
1820 if (ftree_add(argv
[optind
], 0) < 0)
1825 * no read errors allowed on updates/append operation!
1828 while ((str
= get_line(stdin
)) != NULL
) {
1831 if (get_line_error
) {
1832 tty_warn(1, "Problem while reading stdin");
1845 * print out those invalid flag sets found to the user
1849 printflg(unsigned int flg
)
1853 (void)fprintf(stderr
,"%s: Invalid combination of options:", argv0
);
1854 while ((nxt
= ffs(flg
)) != 0) {
1855 flg
&= ~(1 << (nxt
- 1));
1856 (void)fprintf(stderr
, " -%c", flgch
[nxt
- 1]);
1858 (void)putc('\n', stderr
);
1863 * comparison routine used by bsearch to find the format specified
1868 c_frmt(const void *a
, const void *b
)
1870 return strcmp(((const FSUB
*)a
)->name
, ((const FSUB
*)b
)->name
);
1875 * called by format specific options routines to get each format specific
1876 * flag and value specified with -o
1878 * pointer to next OPLIST entry or NULL (end of list).
1886 if ((opt
= ophead
) != NULL
)
1887 ophead
= ophead
->fow
;
1893 * generic routine used to complain about a format specific options
1894 * when the format does not support options.
1905 * print all we were given
1907 tty_warn(1," These format options are not supported for %s",
1909 while ((opt
= opt_next()) != NULL
)
1910 (void)fprintf(stderr
, "\t%s = %s\n", opt
->name
, opt
->value
);
1911 if (strcmp(NM_TAR
, argv0
) == 0)
1914 else if (strcmp(NM_CPIO
, argv0
) == 0)
1924 * breaks the value supplied to -o into a option name and value. options
1925 * are given to -o in the form -o name-value,name=value
1926 * multiple -o may be specified.
1928 * 0 if format in name=value format, -1 if -o is passed junk
1932 opt_add(const char *str
)
1940 if ((str
== NULL
) || (*str
== '\0')) {
1941 tty_warn(0, "Invalid option name");
1944 if ((dstr
= strdup(str
)) == NULL
) {
1945 tty_warn(0, "Unable to allocate space for option list");
1948 frpt
= endpt
= dstr
;
1951 * break into name and values pieces and stuff each one into a
1952 * OPLIST structure. When we know the format, the format specific
1953 * option function will go through this list
1955 while ((frpt
!= NULL
) && (*frpt
!= '\0')) {
1956 if ((endpt
= strchr(frpt
, ',')) != NULL
)
1958 if ((pt
= strchr(frpt
, '=')) == NULL
) {
1959 tty_warn(0, "Invalid options format");
1963 if ((opt
= (OPLIST
*)malloc(sizeof(OPLIST
))) == NULL
) {
1964 tty_warn(0, "Unable to allocate space for option list");
1976 if (ophead
== NULL
) {
1977 optail
= ophead
= opt
;
1988 * Convert an expression of the following forms to an off_t > 0.
1989 * 1) A positive decimal number.
1990 * 2) A positive decimal number followed by a b (mult by 512).
1991 * 3) A positive decimal number followed by a k (mult by 1024).
1992 * 4) A positive decimal number followed by a m (mult by 512).
1993 * 5) A positive decimal number followed by a w (mult by sizeof int)
1994 * 6) Two or more positive decimal numbers (with/without k,b or w).
1995 * separated by x (also * for backwards compatibility), specifying
1996 * the product of the indicated values.
1998 * 0 for an error, a positive value o.w.
2007 num
= STRTOOFFT(val
, &expr
, 0);
2008 if ((num
== OFFT_MAX
) || (num
<= 0) || (expr
== val
))
2048 num
*= str_offt(expr
+ 1);
2064 name
= fgetln(f
, &len
);
2066 get_line_error
= ferror(f
) ? GETLINE_FILE_CORRUPT
: 0;
2069 if (name
[len
-1] != '\n')
2073 get_line_error
= GETLINE_OUT_OF_MEM
;
2076 memcpy(temp
, name
, len
-1);
2083 * for those option functions where the archive format has nothing to do.
2096 * print the usage summary to the user
2103 "usage: pax [-0cdjnvzVO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
2104 " [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
2105 " [pattern ...]\n");
2107 " pax -r [-cdijknuvzADOVYZ] [-E limit] [-f archive] [-N dbdir]\n"
2108 " [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
2109 " [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
2111 " pax -w [-dijtuvzAHLMOPVX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
2112 " [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
2113 " [-U user] ... [-G group] ...\n"
2114 " [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
2116 " pax -r -w [-dijklntuvzADHLMOPVXYZ] [-N dbdir] [-p string] ...\n"
2117 " [-s replstr] ... [-U user] ... [-G group] ...\n"
2118 " [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
2125 * print the usage summary to the user
2131 (void)fputs("usage: tar [-]{crtux}[-befhjklmopqvwzHOPSXZ014578] [archive] "
2133 " [-C directory] [-T file] [-s replstr] "
2134 "[file ...]\n", stderr
);
2142 * print the usage summary to the user
2149 (void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
2150 "[-H format] [-O archive]\n"
2151 " < name-list [> archive]\n"
2152 " cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
2153 "[-F archive] [-H format] \n"
2155 "[pattern ...] [< archive]\n"
2156 " cpio -p [-adlLmuv] destination-directory "
2157 "< name-list\n", stderr
);