1 /* $NetBSD: options.c,v 1.103 2009/02/14 08:10:06 lukem 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.103 2009/02/14 08:10:06 lukem Exp $");
49 #include <sys/types.h>
52 #include <sys/param.h>
55 #if HAVE_NBTOOL_CONFIG_H
56 #include "compat_getopt.h"
76 * Routines which handle command line options
79 static int nopids
; /* tar mode: suppress "pids" for -p option */
80 static char flgch
[] = FLGCH
; /* list of all possible flags (pax) */
81 static OPLIST
*ophead
= NULL
; /* head for format specific options -x */
82 static OPLIST
*optail
= NULL
; /* option tail */
84 static int no_op(void);
85 static void printflg(unsigned int);
86 static int c_frmt(const void *, const void *);
87 static off_t
str_offt(char *);
88 static char *getline(FILE *fp
);
89 static void pax_options(int, char **);
90 static void pax_usage(void);
91 static void tar_options(int, char **);
92 static void tar_usage(void);
94 static void cpio_options(int, char **);
95 static void cpio_usage(void);
98 /* errors from getline */
99 #define GETLINE_FILE_CORRUPT 1
100 #define GETLINE_OUT_OF_MEM 2
101 static int getline_error
;
103 #define BZIP2_CMD "bzip2" /* command to run as bzip2 */
104 #define GZIP_CMD "gzip" /* command to run as gzip */
105 #define COMPRESS_CMD "compress" /* command to run as compress */
110 #define OPT_USE_COMPRESS_PROGRAM 0
111 #define OPT_CHECKPOINT 1
114 #define OPT_ATIME_PRESERVE 4
115 #define OPT_IGNORE_FAILED_READ 5
116 #define OPT_REMOVE_FILES 6
119 #define OPT_VERSION 9
120 #define OPT_EXCLUDE 10
121 #define OPT_BLOCK_COMPRESS 11
122 #define OPT_NORECURSE 12
123 #define OPT_FORCE_LOCAL 13
124 #define OPT_INSECURE 14
125 #define OPT_STRICT 15
126 #define OPT_SPARSE 16
127 #if !HAVE_NBTOOL_CONFIG_H
128 #define OPT_CHROOT 17
132 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
133 * (see pax.h for description of each function)
135 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
136 * read, end_read, st_write, write, end_write, trail,
137 * subtrail, rd_data, wr_data, options
142 /* 0: OLD BINARY CPIO */
143 { "bcpio", 5120, sizeof(HD_BCPIO
), 1, 0, 0, 1, bcpio_id
, cpio_strd
,
144 bcpio_rd
, bcpio_endrd
, cpio_stwr
, bcpio_wr
, cpio_endwr
, NULL
,
145 cpio_subtrail
, rd_wrfile
, wr_rdfile
, bad_opt
},
147 /* 1: OLD OCTAL CHARACTER CPIO */
148 { "cpio", 5120, sizeof(HD_CPIO
), 1, 0, 0, 1, cpio_id
, cpio_strd
,
149 cpio_rd
, cpio_endrd
, cpio_stwr
, cpio_wr
, cpio_endwr
, NULL
,
150 cpio_subtrail
, rd_wrfile
, wr_rdfile
, bad_opt
},
152 /* 2: SVR4 HEX CPIO */
153 { "sv4cpio", 5120, sizeof(HD_VCPIO
), 1, 0, 0, 1, vcpio_id
, cpio_strd
,
154 vcpio_rd
, vcpio_endrd
, cpio_stwr
, vcpio_wr
, cpio_endwr
, NULL
,
155 cpio_subtrail
, rd_wrfile
, wr_rdfile
, bad_opt
},
157 /* 3: SVR4 HEX CPIO WITH CRC */
158 { "sv4crc", 5120, sizeof(HD_VCPIO
), 1, 0, 0, 1, crc_id
, crc_strd
,
159 vcpio_rd
, vcpio_endrd
, crc_stwr
, vcpio_wr
, cpio_endwr
, NULL
,
160 cpio_subtrail
, rd_wrfile
, wr_rdfile
, bad_opt
},
163 { "tar", 10240, BLKMULT
, 0, 1, BLKMULT
, 0, tar_id
, no_op
,
164 tar_rd
, tar_endrd
, no_op
, tar_wr
, tar_endwr
, tar_trail
,
165 NULL
, rd_wrfile
, wr_rdfile
, tar_opt
},
168 { "ustar", 10240, BLKMULT
, 0, 1, BLKMULT
, 0, ustar_id
, ustar_strd
,
169 ustar_rd
, tar_endrd
, ustar_stwr
, ustar_wr
, tar_endwr
, tar_trail
,
170 NULL
, rd_wrfile
, wr_rdfile
, bad_opt
}
173 #define F_BCPIO 0 /* old binary cpio format */
174 #define F_CPIO 1 /* old octal character cpio format */
175 #define F_SV4CPIO 2 /* SVR4 hex cpio format */
176 #define F_SV4CRC 3 /* SVR4 hex with crc cpio format */
177 #define F_TAR 4 /* old V7 UNIX tar format */
178 #define F_USTAR 5 /* ustar format */
180 #define F_TAR 0 /* old V7 UNIX tar format */
181 #define F_USTAR 1 /* ustar format */
183 #define DEFLT F_USTAR /* default write format from list above */
186 * ford is the archive search order used by get_arc() to determine what kind
187 * of archive we are dealing with. This helps to properly id archive formats
188 * some formats may be subsets of others....
190 int ford
[] = {F_USTAR
, F_TAR
,
192 F_SV4CRC
, F_SV4CPIO
, F_CPIO
, F_BCPIO
,
197 * filename record separator
202 * Do we have -C anywhere?
208 * figure out if we are pax, tar or cpio. Call the appropriate options
213 options(int argc
, char **argv
)
217 * Are we acting like pax, tar or cpio (based on argv[0])
219 if ((argv0
= strrchr(argv
[0], '/')) != NULL
)
224 if (strstr(argv0
, NM_TAR
)) {
226 tar_options(argc
, argv
);
228 } else if (strstr(argv0
, NM_CPIO
)) {
230 cpio_options(argc
, argv
);
234 pax_options(argc
, argv
);
238 struct option pax_longopts
[] = {
239 { "insecure", no_argument
, 0,
241 { "force-local", no_argument
, 0,
249 * look at the user specified flags. set globals as required and check if
250 * the user specified a legal set of flags. If not, complain and exit
254 pax_options(int argc
, char **argv
)
264 * process option flags
266 while ((c
= getopt_long(argc
, argv
,
267 "0ab:cdf:ijklno:p:rs:tuvwx:zAB:DE:G:HLMN:OPT:U:VXYZ",
268 pax_longopts
, NULL
)) != -1) {
284 if ((wrblksz
= (int)str_offt(optarg
)) <= 0) {
285 tty_warn(1, "Invalid block size %s", optarg
);
291 * inverse match on patterns
298 * match only dir on extract, not the subtree at dir
305 * filename where the archive is stored
312 * interactive file rename
322 gzip_program
= BZIP2_CMD
;
326 * do not clobber files that exist
333 * try to link src to dest with copy (-rw)
340 * select first match for a pattern only
347 * pass format specific options
350 if (opt_add(optarg
) < 0)
355 * specify file characteristic options
357 for (pt
= optarg
; *pt
!= '\0'; ++pt
) {
361 * do not preserve access time
367 * preserve user id, group id, file
368 * mode, access/modification times
380 * do not preserve file flags
387 * do not preserve modification time
399 * preserve file mode bits
404 tty_warn(1, "Invalid -p string: %c",
420 * file name substitution name pattern
422 if (rep_add(optarg
) < 0) {
430 * preserve access time on filesystem nodes we read
437 * ignore those older files
444 * verbose operation mode
457 * specify an archive format on write
460 frmt
= (FSUB
*)bsearch((void *)&tmp
, (void *)fsub
,
461 sizeof(fsub
)/sizeof(FSUB
), sizeof(FSUB
), c_frmt
);
466 tty_warn(1, "Unknown -x format: %s", optarg
);
467 (void)fputs("pax: Known -x formats are:", stderr
);
468 for (i
= 0; i
< (sizeof(fsub
)/sizeof(FSUB
)); ++i
)
469 (void)fprintf(stderr
, " %s", fsub
[i
].name
);
470 (void)fputs("\n\n", stderr
);
475 * use gzip. Non standard option.
477 gzip_program
= GZIP_CMD
;
485 * non-standard option on number of bytes written on a
486 * single archive volume.
488 if ((wrlimit
= str_offt(optarg
)) <= 0) {
489 tty_warn(1, "Invalid write limit %s", optarg
);
492 if (wrlimit
% BLKMULT
) {
494 "Write limit is not a %d byte multiple",
502 * On extraction check file inode change time before the
503 * modification of the file name. Non standard option.
510 * non-standard limit on read faults
511 * 0 indicates stop after first error, values
512 * indicate a limit, "none" try forever
515 if (strcmp(none
, optarg
) == 0)
517 else if ((maxflt
= atoi(optarg
)) < 0) {
519 "Error count value must be positive");
525 * non-standard option for selecting files within an
526 * archive by group (gid or name)
528 if (grp_add(optarg
) < 0) {
536 * follow command line symlinks only
551 tty_warn(1, "Support for -%c is not compiled in", c
);
556 * Treat list of filenames on stdin as an
557 * mtree(8) specfile. Non standard option.
564 * Use alternative directory for user db lookups.
566 if (!setup_getid(optarg
)) {
568 "Unable to use user and group databases in `%s'",
576 * Force one volume. Non standard option.
578 force_one_volume
= 1;
582 * do NOT follow symlinks (default)
589 * non-standard option for selecting files within an
590 * archive by modification time range (lower,upper)
592 if (trng_add(optarg
) < 0) {
600 * non-standard option for selecting files within an
601 * archive by user (uid or name)
603 if (usr_add(optarg
) < 0) {
611 * somewhat verbose operation mode (no listing)
618 * do not pass over mount points in the file system
625 * On extraction check file inode change time after the
626 * modification of the file name. Non standard option.
633 * On extraction check modification time after the
634 * modification of the file name. Non standard option.
642 case OPT_FORCE_LOCAL
:
653 * figure out the operation mode of pax read,write,extract,copy,append
654 * or list. check that we have not been given a bogus set of flags
655 * for the operation mode.
661 } else if (ISEXTRACT(flg
)) {
664 } else if (ISARCHIVE(flg
)) {
667 } else if (ISAPPND(flg
)) {
670 } else if (ISCOPY(flg
)) {
681 * if we are writing (ARCHIVE) we use the default format if the user
682 * did not specify a format. when we write during an APPEND, we will
683 * adopt the format of the existing archive if none was supplied.
685 if (!(flg
& XF
) && (act
== ARCHIVE
))
686 frmt
= &(fsub
[DEFLT
]);
689 * process the args as they are interpreted by the operation mode
694 for (; optind
< argc
; optind
++)
695 if (pat_add(argv
[optind
], NULL
, NOGLOB_MTCH
) < 0)
699 if (optind
>= argc
) {
700 tty_warn(0, "Destination directory was not supplied");
705 if (mkpath(dirptr
) < 0)
710 for (; optind
< argc
; optind
++)
711 if (ftree_add(argv
[optind
], 0) < 0)
714 * no read errors allowed on updates/append operation!
724 * look at the user specified flags. set globals as required and check if
725 * the user specified a legal set of flags. If not, complain and exit
728 struct option tar_longopts
[] = {
729 { "block-size", required_argument
, 0, 'b' },
730 { "bunzip2", no_argument
, 0, 'j' },
731 { "bzip2", no_argument
, 0, 'j' },
732 { "create", no_argument
, 0, 'c' }, /* F */
733 /* -e -- no corresponding long option */
734 { "file", required_argument
, 0, 'f' },
735 { "dereference", no_argument
, 0, 'h' },
736 { "keep-old-files", no_argument
, 0, 'k' },
737 { "one-file-system", no_argument
, 0, 'l' },
738 { "modification-time", no_argument
, 0, 'm' },
739 { "old-archive", no_argument
, 0, 'o' },
740 { "portability", no_argument
, 0, 'o' },
741 { "same-permissions", no_argument
, 0, 'p' },
742 { "preserve-permissions", no_argument
, 0, 'p' },
743 { "preserve", no_argument
, 0, 'p' },
744 { "fast-read", no_argument
, 0, 'q' },
745 { "append", no_argument
, 0, 'r' }, /* F */
746 { "update", no_argument
, 0, 'u' }, /* F */
747 { "list", no_argument
, 0, 't' }, /* F */
748 { "verbose", no_argument
, 0, 'v' },
749 { "interactive", no_argument
, 0, 'w' },
750 { "confirmation", no_argument
, 0, 'w' },
751 { "extract", no_argument
, 0, 'x' }, /* F */
752 { "get", no_argument
, 0, 'x' }, /* F */
753 { "gzip", no_argument
, 0, 'z' },
754 { "gunzip", no_argument
, 0, 'z' },
755 { "read-full-blocks", no_argument
, 0, 'B' },
756 { "directory", required_argument
, 0, 'C' },
757 { "to-stdout", no_argument
, 0, 'O' },
758 { "absolute-paths", no_argument
, 0, 'P' },
759 { "sparse", no_argument
, 0, 'S' },
760 { "files-from", required_argument
, 0, 'T' },
761 { "summary", no_argument
, 0, 'V' },
762 { "stats", no_argument
, 0, 'V' },
763 { "exclude-from", required_argument
, 0, 'X' },
764 { "compress", no_argument
, 0, 'Z' },
765 { "uncompress", no_argument
, 0, 'Z' },
766 { "strict", no_argument
, 0,
768 { "atime-preserve", no_argument
, 0,
769 OPT_ATIME_PRESERVE
},
770 { "unlink", no_argument
, 0,
772 { "use-compress-program", required_argument
, 0,
773 OPT_USE_COMPRESS_PROGRAM
},
774 { "force-local", no_argument
, 0,
776 { "insecure", no_argument
, 0,
778 { "exclude", required_argument
, 0,
780 { "no-recursion", no_argument
, 0,
782 #if !HAVE_NBTOOL_CONFIG_H
783 { "chroot", no_argument
, 0,
786 #if 0 /* Not implemented */
787 { "catenate", no_argument
, 0, 'A' }, /* F */
788 { "concatenate", no_argument
, 0, 'A' }, /* F */
789 { "diff", no_argument
, 0, 'd' }, /* F */
790 { "compare", no_argument
, 0, 'd' }, /* F */
791 { "checkpoint", no_argument
, 0,
793 { "help", no_argument
, 0,
795 { "info-script", required_argument
, 0, 'F' },
796 { "new-volume-script", required_argument
, 0, 'F' },
797 { "incremental", no_argument
, 0, 'G' },
798 { "listed-incremental", required_argument
, 0, 'g' },
799 { "ignore-zeros", no_argument
, 0, 'i' },
800 { "ignore-failed-read", no_argument
, 0,
801 OPT_IGNORE_FAILED_READ
},
802 { "starting-file", no_argument
, 0, 'K' },
803 { "tape-length", required_argument
, 0, 'L' },
804 { "multi-volume", no_argument
, 0, 'M' },
805 { "after-date", required_argument
, 0, 'N' },
806 { "newer", required_argument
, 0, 'N' },
807 { "record-number", no_argument
, 0, 'R' },
808 { "remove-files", no_argument
, 0,
810 { "same-order", no_argument
, 0, 's' },
811 { "preserve-order", no_argument
, 0, 's' },
812 { "null", no_argument
, 0,
814 { "totals", no_argument
, 0,
816 { "volume-name", required_argument
, 0, 'V' }, /* XXX */
817 { "label", required_argument
, 0, 'V' }, /* XXX */
818 { "version", no_argument
, 0,
820 { "verify", no_argument
, 0, 'W' },
821 { "block-compress", no_argument
, 0,
822 OPT_BLOCK_COMPRESS
},
828 tar_set_action(int op
)
830 if (act
!= ERROR
&& act
!= op
)
836 tar_options(int argc
, char **argv
)
842 int incfiles_max
= 0;
847 struct incfile
*incfiles
= NULL
;
850 * Set default values.
856 * process option flags
858 while ((c
= getoldopt(argc
, argv
,
859 "+b:cef:hjklmopqrs:tuvwxzBC:HI:OPST:X:Z014578",
865 * specify blocksize in 512-byte blocks
867 if ((wrblksz
= (int)str_offt(optarg
)) <= 0) {
868 tty_warn(1, "Invalid block size %s", optarg
);
871 wrblksz
*= 512; /* XXX - check for int oflow */
877 tar_set_action(ARCHIVE
);
881 * stop after first error
887 * filename where the archive is stored
889 if ((optarg
[0] == '-') && (optarg
[1]== '\0')) {
908 * pass through bzip2. not a standard option
911 gzip_program
= BZIP2_CMD
;
915 * do not clobber files that exist
921 * do not pass over mount points in the file system
927 * do not preserve modification time
933 * This option does several things based on whether
934 * this is a create or extract operation.
936 if (act
== ARCHIVE
) {
937 /* GNU tar: write V7 format archives. */
939 /* 4.2BSD: don't add directory entries. */
940 if (opt_add("write_opt=nodir") < 0)
944 /* SUS: don't preserve owner/group. */
954 * preserve user id, group id, file
955 * mode, access/modification times
965 * select first match for a pattern only
972 * append to the archive
974 tar_set_action(APPND
);
978 * file name substitution name pattern
980 if (rep_add(optarg
) < 0) {
987 * list contents of the tape
989 tar_set_action(LIST
);
993 * verbose operation mode
999 * interactive file rename
1005 * extract an archive, preserving mode,
1006 * and mtime if possible.
1008 tar_set_action(EXTRACT
);
1013 * use gzip. Non standard option.
1016 gzip_program
= GZIP_CMD
;
1020 * Nothing to do here, this is pax default
1029 * follow command line symlinks only
1035 if (++nincfiles
> incfiles_max
) {
1036 incfiles_max
= nincfiles
+ 3;
1037 incfiles
= realloc(incfiles
,
1038 sizeof(*incfiles
) * incfiles_max
);
1039 if (incfiles
== NULL
) {
1040 tty_warn(0, "Unable to allocate space "
1045 incfiles
[nincfiles
- 1].file
= optarg
;
1046 incfiles
[nincfiles
- 1].dir
= chdname
;
1050 * do not remove leading '/' from pathnames
1056 /* do nothing; we already generate sparse files */
1060 * semi-verbose operation mode (no listing)
1066 * GNU tar compat: exclude the files listed in optarg
1068 if (tar_gnutar_X_compat(optarg
) != 0)
1076 gzip_program
= COMPRESS_CMD
;
1096 case OPT_ATIME_PRESERVE
:
1100 /* Just ignore -- we always unlink first. */
1102 case OPT_USE_COMPRESS_PROGRAM
:
1104 gzip_program
= optarg
;
1106 case OPT_FORCE_LOCAL
:
1113 /* disable gnu extensions */
1117 if (tar_gnutar_minus_minus_exclude(optarg
) != 0)
1123 #if !HAVE_NBTOOL_CONFIG_H
1136 /* Tar requires an action. */
1140 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
1141 if (fstdin
== 1 && act
== ARCHIVE
)
1146 /* Traditional tar behaviour (pax wants to read file list from stdin) */
1147 if ((act
== ARCHIVE
|| act
== APPND
) && argc
== 0 && nincfiles
== 0)
1150 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
1151 * (unless -o specified)
1153 if (act
== ARCHIVE
|| act
== APPND
)
1154 frmt
= &(fsub
[Oflag
? F_TAR
: F_USTAR
]);
1159 tty_warn(1, "The -O/-o options are only valid when "
1160 "writing or extracting an archive");
1166 * process the args as they are interpreted by the operation mode
1175 char *file
, *dir
= NULL
;
1176 int mustfreedir
= 0;
1178 while (nincfiles
|| *argv
!= NULL
) {
1180 * If we queued up any include files,
1181 * pull them in now. Otherwise, check
1182 * for -I and -C positional flags.
1183 * Anything else must be a file to
1187 file
= incfiles
->file
;
1188 dir
= incfiles
->dir
;
1192 } else if (strcmp(*argv
, "-I") == 0) {
1193 if (*++argv
== NULL
)
1207 if (strcmp(file
, "-") == 0)
1209 else if ((fp
= fopen(file
, "r")) == NULL
) {
1210 tty_warn(1, "Unable to open file '%s' for read", file
);
1213 while ((str
= getline(fp
)) != NULL
) {
1215 if (dir
&& mustfreedir
)
1222 if (strcmp(str
, "-C") == 0) {
1228 if (strncmp(str
, "-C ", 3) == 0) {
1230 if (dir
&& mustfreedir
)
1232 dir
= strdup(str
+ 3);
1237 if (pat_add(str
, dir
, NOGLOB_MTCH
) < 0)
1241 /* Bomb if given -C w/out a dir. */
1244 if (dir
&& mustfreedir
)
1246 if (strcmp(file
, "-") != 0)
1248 if (getline_error
) {
1249 tty_warn(1, "Problem with file '%s'", file
);
1252 } else if (strcmp(*argv
, "-C") == 0) {
1253 if (*++argv
== NULL
)
1257 } else if (pat_add(*argv
++, chdname
, 0) < 0)
1263 * if patterns were added, we are doing chdir()
1264 * on a file-by-file basis, else, just one
1265 * global chdir (if any) after opening input.
1273 if (chdname
!= NULL
) { /* initial chdir() */
1274 if (ftree_add(chdname
, 1) < 0)
1278 while (nincfiles
|| *argv
!= NULL
) {
1282 * If we queued up any include files, pull them in
1283 * now. Otherwise, check for -I and -C positional
1284 * flags. Anything else must be a file to include
1288 file
= incfiles
->file
;
1289 dir
= incfiles
->dir
;
1292 } else if (strcmp(*argv
, "-I") == 0) {
1293 if (*++argv
== NULL
)
1306 /* Set directory if needed */
1308 if (ftree_add(dir
, 1) < 0)
1312 if (strcmp(file
, "-") == 0)
1314 else if ((fp
= fopen(file
, "r")) == NULL
) {
1315 tty_warn(1, "Unable to open file '%s' for read", file
);
1318 while ((str
= getline(fp
)) != NULL
) {
1320 if (ftree_add(str
, 1) < 0)
1325 if (strcmp(str
, "-C") == 0) {
1329 if (strncmp(str
, "-C ", 3) == 0) {
1330 if (ftree_add(str
+ 3, 1) < 0)
1334 if (ftree_add(str
, 0) < 0)
1337 /* Bomb if given -C w/out a dir. */
1340 if (strcmp(file
, "-") != 0)
1342 if (getline_error
) {
1343 tty_warn(1, "Problem with file '%s'",
1347 } else if (strcmp(*argv
, "-C") == 0) {
1348 if (*++argv
== NULL
)
1350 if (ftree_add(*argv
++, 1) < 0)
1352 } else if (ftree_add(*argv
++, 0) < 0)
1356 * no read errors allowed on updates/append operation!
1361 if (!fstdin
&& ((arcname
== (char *)NULL
) || (*arcname
== '\0'))) {
1362 arcname
= getenv("TAPE");
1363 if ((arcname
== NULL
) || (*arcname
== '\0'))
1364 arcname
= _PATH_DEFTAPE
;
1378 slash
+= strspn(slash
, "/");
1379 slash
+= strcspn(slash
, "/");
1381 done
= (*slash
== '\0');
1384 if (domkdir(path
, 0777) == -1)
1393 /* Can't create or or not a directory */
1394 syswarn(1, errno
, "Cannot create directory `%s'", path
);
1400 struct option cpio_longopts
[] = {
1401 { "reset-access-time", no_argument
, 0, 'a' },
1402 { "make-directories", no_argument
, 0, 'd' },
1403 { "nonmatching", no_argument
, 0, 'f' },
1404 { "extract", no_argument
, 0, 'i' },
1405 { "link", no_argument
, 0, 'l' },
1406 { "preserve-modification-time", no_argument
, 0, 'm' },
1407 { "create", no_argument
, 0, 'o' },
1408 { "pass-through", no_argument
, 0, 'p' },
1409 { "rename", no_argument
, 0, 'r' },
1410 { "list", no_argument
, 0, 't' },
1411 { "unconditional", no_argument
, 0, 'u' },
1412 { "verbose", no_argument
, 0, 'v' },
1413 { "append", no_argument
, 0, 'A' },
1414 { "pattern-file", required_argument
, 0, 'E' },
1415 { "file", required_argument
, 0, 'F' },
1416 { "force-local", no_argument
, 0,
1418 { "format", required_argument
, 0, 'H' },
1419 { "dereference", no_argument
, 0, 'L' },
1420 { "swap-halfwords", no_argument
, 0, 'S' },
1421 { "summary", no_argument
, 0, 'V' },
1422 { "stats", no_argument
, 0, 'V' },
1423 { "insecure", no_argument
, 0,
1425 { "sparse", no_argument
, 0,
1429 /* Not implemented */
1430 { "null", no_argument
, 0, '0' },
1431 { "swap", no_argument
, 0, 'b' },
1432 { "numeric-uid-gid", no_argument
, 0, 'n' },
1433 { "swap-bytes", no_argument
, 0, 's' },
1434 { "message", required_argument
, 0, 'M' },
1435 { "owner", required_argument
, 0 'R
' },
1436 { "dot", no_argument, 0, 'V
' }, /* xxx */
1437 { "block-size", required_argument, 0,
1439 { "no-absolute-pathnames", no_argument, 0,
1440 OPT_NO_ABSOLUTE_PATHNAMES },
1441 { "no-preserve-owner", no_argument, 0,
1442 OPT_NO_PRESERVE_OWNER },
1443 { "only-verify-crc", no_argument, 0,
1444 OPT_ONLY_VERIFY_CRC },
1445 { "rsh-command", required_argument, 0,
1447 { "version", no_argument, 0,
1454 cpio_set_action(int op)
1456 if ((act == APPND && op == ARCHIVE) || (act == ARCHIVE && op == APPND))
1458 else if (act != ERROR && act != op)
1466 * look at the user specified flags. set globals as required and check if
1467 * the user specified a legal set of flags. If not, complain and exit
1471 cpio_options(int argc, char **argv)
1490 * process option flags
1492 while ((c = getoldopt(argc, argv,
1493 "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
1494 cpio_longopts, NULL)) != -1) {
1498 * preserve access time on filesystem nodes we read
1506 * swap bytes and half-words when reading data
1514 frmt = &fsub[F_SV4CPIO];
1518 * create directories as needed
1519 * pax does this by default ..
1525 * inverse match on patterns
1534 cpio_set_action(EXTRACT);
1543 * try to link src to dest with copy (-rw)
1559 cpio_set_action(ARCHIVE);
1560 frmt = &(fsub[F_SV4CRC]);
1565 * cpio -p is like pax -rw
1567 cpio_set_action(COPY);
1572 * interactive file rename
1580 * swap bytes after reading data
1586 * list contents of archive
1588 cpio_set_action(LIST);
1594 * don't ignore those older files
1602 * verbose operation mode
1609 * use gzip. Non standard option.
1611 gzip_program
= GZIP_CMD
;
1615 * append to an archive
1617 cpio_set_action(APPND
);
1622 * set blocksize to 5120
1630 if ((blksz
= (int)str_offt(optarg
)) <= 0) {
1631 tty_warn(1, "Invalid block size %s", optarg
);
1637 * file with patterns to extract or list
1639 if ((fp
= fopen(optarg
, "r")) == NULL
) {
1640 tty_warn(1, "Unable to open file '%s' for read",
1644 while ((str
= getline(fp
)) != NULL
) {
1645 pat_add(str
, NULL
, 0);
1648 if (getline_error
) {
1649 tty_warn(1, "Problem with file '%s'", optarg
);
1655 * specify an archive format on write
1658 frmt
= (FSUB
*)bsearch((void *)&tmp
, (void *)fsub
,
1659 sizeof(fsub
)/sizeof(FSUB
), sizeof(FSUB
), c_frmt
);
1664 tty_warn(1, "Unknown -H format: %s", optarg
);
1665 (void)fputs("cpio: Known -H formats are:", stderr
);
1666 for (i
= 0; i
< (sizeof(fsub
)/sizeof(FSUB
)); ++i
)
1667 (void)fprintf(stderr
, " %s", fsub
[i
].name
);
1668 (void)fputs("\n\n", stderr
);
1674 * filename where the archive is stored
1676 if ((optarg
[0] == '-') && (optarg
[1]== '\0')) {
1678 * treat a - as stdin
1702 * swap halfwords after reading data
1707 case 'V': /* print a '.' for each file processed */
1712 * semi-verbose operation mode (no listing)
1719 * use compress. Non standard option.
1721 gzip_program
= COMPRESS_CMD
;
1725 * process Version 6 cpio format
1727 frmt
= &(fsub
[F_BCPIO
]);
1728 case OPT_FORCE_LOCAL
:
1736 /* do nothing; we already generate sparse files */
1745 * figure out the operation mode of cpio. check that we have not been
1746 * given a bogus set of flags for the operation mode.
1750 bflg
= flg
& BDLIST
;
1751 } else if (ISEXTRACT(flg
)) {
1753 bflg
= flg
& BDEXTR
;
1754 } else if (ISARCHIVE(flg
)) {
1756 bflg
= flg
& BDARCH
;
1757 } else if (ISAPPND(flg
)) {
1759 bflg
= flg
& BDARCH
;
1760 } else if (ISCOPY(flg
)) {
1762 bflg
= flg
& BDCOPY
;
1770 * if we are writing (ARCHIVE) we use the default format if the user
1771 * did not specify a format. when we write during an APPEND, we will
1772 * adopt the format of the existing archive if none was supplied.
1774 if (!(flg
& XF
) && (act
== ARCHIVE
))
1775 frmt
= &(fsub
[F_BCPIO
]);
1778 * process the args as they are interpreted by the operation mode
1783 for (; optind
< argc
; optind
++)
1784 if (pat_add(argv
[optind
], NULL
, 0) < 0)
1788 if (optind
>= argc
) {
1789 tty_warn(0, "Destination directory was not supplied");
1793 dirptr
= argv
[argc
];
1797 if (argc
!= optind
) {
1798 for (; optind
< argc
; optind
++)
1799 if (ftree_add(argv
[optind
], 0) < 0)
1804 * no read errors allowed on updates/append operation!
1807 while ((str
= getline(stdin
)) != NULL
) {
1810 if (getline_error
) {
1811 tty_warn(1, "Problem while reading stdin");
1824 * print out those invalid flag sets found to the user
1828 printflg(unsigned int flg
)
1832 (void)fprintf(stderr
,"%s: Invalid combination of options:", argv0
);
1833 while ((nxt
= ffs(flg
)) != 0) {
1834 flg
&= ~(1 << (nxt
- 1));
1835 (void)fprintf(stderr
, " -%c", flgch
[nxt
- 1]);
1837 (void)putc('\n', stderr
);
1842 * comparison routine used by bsearch to find the format specified
1847 c_frmt(const void *a
, const void *b
)
1849 return strcmp(((const FSUB
*)a
)->name
, ((const FSUB
*)b
)->name
);
1854 * called by format specific options routines to get each format specific
1855 * flag and value specified with -o
1857 * pointer to next OPLIST entry or NULL (end of list).
1865 if ((opt
= ophead
) != NULL
)
1866 ophead
= ophead
->fow
;
1872 * generic routine used to complain about a format specific options
1873 * when the format does not support options.
1884 * print all we were given
1886 tty_warn(1," These format options are not supported for %s",
1888 while ((opt
= opt_next()) != NULL
)
1889 (void)fprintf(stderr
, "\t%s = %s\n", opt
->name
, opt
->value
);
1890 if (strcmp(NM_TAR
, argv0
) == 0)
1893 else if (strcmp(NM_CPIO
, argv0
) == 0)
1903 * breaks the value supplied to -o into a option name and value. options
1904 * are given to -o in the form -o name-value,name=value
1905 * multiple -o may be specified.
1907 * 0 if format in name=value format, -1 if -o is passed junk
1911 opt_add(const char *str
)
1919 if ((str
== NULL
) || (*str
== '\0')) {
1920 tty_warn(0, "Invalid option name");
1923 if ((dstr
= strdup(str
)) == NULL
) {
1924 tty_warn(0, "Unable to allocate space for option list");
1927 frpt
= endpt
= dstr
;
1930 * break into name and values pieces and stuff each one into a
1931 * OPLIST structure. When we know the format, the format specific
1932 * option function will go through this list
1934 while ((frpt
!= NULL
) && (*frpt
!= '\0')) {
1935 if ((endpt
= strchr(frpt
, ',')) != NULL
)
1937 if ((pt
= strchr(frpt
, '=')) == NULL
) {
1938 tty_warn(0, "Invalid options format");
1942 if ((opt
= (OPLIST
*)malloc(sizeof(OPLIST
))) == NULL
) {
1943 tty_warn(0, "Unable to allocate space for option list");
1955 if (ophead
== NULL
) {
1956 optail
= ophead
= opt
;
1967 * Convert an expression of the following forms to an off_t > 0.
1968 * 1) A positive decimal number.
1969 * 2) A positive decimal number followed by a b (mult by 512).
1970 * 3) A positive decimal number followed by a k (mult by 1024).
1971 * 4) A positive decimal number followed by a m (mult by 512).
1972 * 5) A positive decimal number followed by a w (mult by sizeof int)
1973 * 6) Two or more positive decimal numbers (with/without k,b or w).
1974 * separated by x (also * for backwards compatibility), specifying
1975 * the product of the indicated values.
1977 * 0 for an error, a positive value o.w.
1986 num
= STRTOOFFT(val
, &expr
, 0);
1987 if ((num
== OFFT_MAX
) || (num
<= 0) || (expr
== val
))
2027 num
*= str_offt(expr
+ 1);
2043 name
= fgetln(f
, &len
);
2045 getline_error
= ferror(f
) ? GETLINE_FILE_CORRUPT
: 0;
2048 if (name
[len
-1] != '\n')
2052 getline_error
= GETLINE_OUT_OF_MEM
;
2055 memcpy(temp
, name
, len
-1);
2062 * for those option functions where the archive format has nothing to do.
2075 * print the usage summary to the user
2082 "usage: pax [-0cdjnvzVO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
2083 " [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
2084 " [pattern ...]\n");
2086 " pax -r [-cdijknuvzADOVYZ] [-E limit] [-f archive] [-N dbdir]\n"
2087 " [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
2088 " [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
2090 " pax -w [-dijtuvzAHLMOPVX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
2091 " [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
2092 " [-U user] ... [-G group] ...\n"
2093 " [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
2095 " pax -r -w [-dijklntuvzADHLMOPVXYZ] [-N dbdir] [-p string] ...\n"
2096 " [-s replstr] ... [-U user] ... [-G group] ...\n"
2097 " [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
2104 * print the usage summary to the user
2110 (void)fputs("usage: tar [-]{crtux}[-befhjklmopqvwzHOPSXZ014578] [archive] "
2112 " [-C directory] [-T file] [-s replstr] "
2113 "[file ...]\n", stderr
);
2121 * print the usage summary to the user
2128 (void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
2129 "[-H format] [-O archive]\n"
2130 " < name-list [> archive]\n"
2131 " cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
2132 "[-F archive] [-H format] \n"
2134 "[pattern ...] [< archive]\n"
2135 " cpio -p [-adlLmuv] destination-directory "
2136 "< name-list\n", stderr
);