Home | History | Annotate | Line # | Download | only in pax
options.c revision 1.118.8.1
      1  1.118.8.1    martin /*	$NetBSD: options.c,v 1.118.8.1 2021/05/07 17:40:31 martin Exp $	*/
      2        1.5       cgd 
      3        1.1       jtc /*-
      4       1.67       agc  * Copyright (c) 1992 Keith Muller.
      5        1.1       jtc  * Copyright (c) 1992, 1993
      6        1.1       jtc  *	The Regents of the University of California.  All rights reserved.
      7        1.1       jtc  *
      8        1.1       jtc  * This code is derived from software contributed to Berkeley by
      9        1.1       jtc  * Keith Muller of the University of California, San Diego.
     10        1.1       jtc  *
     11        1.1       jtc  * Redistribution and use in source and binary forms, with or without
     12        1.1       jtc  * modification, are permitted provided that the following conditions
     13        1.1       jtc  * are met:
     14        1.1       jtc  * 1. Redistributions of source code must retain the above copyright
     15        1.1       jtc  *    notice, this list of conditions and the following disclaimer.
     16        1.1       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1       jtc  *    notice, this list of conditions and the following disclaimer in the
     18        1.1       jtc  *    documentation and/or other materials provided with the distribution.
     19       1.66       agc  * 3. Neither the name of the University nor the names of its contributors
     20       1.66       agc  *    may be used to endorse or promote products derived from this software
     21       1.66       agc  *    without specific prior written permission.
     22       1.66       agc  *
     23       1.66       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.66       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.66       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.66       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.66       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.66       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.66       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.66       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.66       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.66       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.66       agc  * SUCH DAMAGE.
     34       1.66       agc  */
     35       1.66       agc 
     36       1.69     lukem #if HAVE_NBTOOL_CONFIG_H
     37       1.69     lukem #include "nbtool_config.h"
     38       1.69     lukem #endif
     39       1.69     lukem 
     40        1.8  christos #include <sys/cdefs.h>
     41       1.69     lukem #if !defined(lint)
     42        1.5       cgd #if 0
     43        1.5       cgd static char sccsid[] = "@(#)options.c	8.2 (Berkeley) 4/18/94";
     44        1.5       cgd #else
     45  1.118.8.1    martin __RCSID("$NetBSD: options.c,v 1.118.8.1 2021/05/07 17:40:31 martin Exp $");
     46        1.5       cgd #endif
     47        1.1       jtc #endif /* not lint */
     48        1.1       jtc 
     49        1.1       jtc #include <sys/types.h>
     50        1.1       jtc #include <sys/time.h>
     51        1.1       jtc #include <sys/stat.h>
     52        1.1       jtc #include <sys/param.h>
     53       1.38        tv #include <ctype.h>
     54       1.40  christos #include <errno.h>
     55       1.76       jmc #if HAVE_NBTOOL_CONFIG_H
     56       1.76       jmc #include "compat_getopt.h"
     57       1.76       jmc #else
     58       1.38        tv #include <getopt.h>
     59       1.76       jmc #endif
     60       1.38        tv #include <limits.h>
     61        1.1       jtc #include <stdio.h>
     62       1.38        tv #include <stdlib.h>
     63        1.1       jtc #include <string.h>
     64        1.1       jtc #include <unistd.h>
     65      1.114  christos #include <inttypes.h>
     66       1.40  christos #include <paths.h>
     67      1.117  christos #include <util.h>
     68        1.1       jtc #include "pax.h"
     69        1.1       jtc #include "options.h"
     70        1.1       jtc #include "cpio.h"
     71        1.1       jtc #include "tar.h"
     72        1.1       jtc #include "extern.h"
     73       1.39     lukem #ifndef SMALL
     74       1.35     lukem #include "mtree.h"
     75       1.39     lukem #endif	/* SMALL */
     76        1.1       jtc 
     77  1.118.8.1    martin char *chdname;
     78  1.118.8.1    martin #if !HAVE_NBTOOL_CONFIG_H
     79  1.118.8.1    martin int do_chroot;
     80  1.118.8.1    martin #endif
     81  1.118.8.1    martin 
     82        1.1       jtc /*
     83        1.1       jtc  * Routines which handle command line options
     84        1.1       jtc  */
     85      1.117  christos struct stat tst;		/* Timestamp to set if non-0 */
     86        1.1       jtc 
     87       1.18        tv static int nopids;		/* tar mode: suppress "pids" for -p option */
     88       1.74  christos static char flgch[] = FLGCH;	/* list of all possible flags (pax) */
     89        1.1       jtc static OPLIST *ophead = NULL;	/* head for format specific options -x */
     90        1.1       jtc static OPLIST *optail = NULL;	/* option tail */
     91        1.1       jtc 
     92      1.111     joerg static int opt_add(const char *);
     93       1.33     lukem static int no_op(void);
     94       1.33     lukem static void printflg(unsigned int);
     95       1.33     lukem static int c_frmt(const void *, const void *);
     96       1.33     lukem static off_t str_offt(char *);
     97      1.105       roy static char *get_line(FILE *fp);
     98      1.117  christos #ifndef SMALL
     99      1.117  christos static int set_tstamp(const char *, struct stat *);
    100      1.117  christos #endif
    101       1.33     lukem static void pax_options(int, char **);
    102      1.111     joerg __dead static void pax_usage(void);
    103       1.33     lukem static void tar_options(int, char **);
    104      1.111     joerg __dead static void tar_usage(void);
    105       1.89  christos #ifndef NO_CPIO
    106       1.33     lukem static void cpio_options(int, char **);
    107      1.111     joerg __dead static void cpio_usage(void);
    108       1.89  christos #endif
    109        1.1       jtc 
    110      1.105       roy /* errors from get_line */
    111       1.40  christos #define GETLINE_FILE_CORRUPT 1
    112       1.40  christos #define GETLINE_OUT_OF_MEM 2
    113      1.105       roy static int get_line_error;
    114       1.23        is 
    115       1.62       wiz #define BZIP2_CMD	"bzip2"		/* command to run as bzip2 */
    116        1.6       mrg #define GZIP_CMD	"gzip"		/* command to run as gzip */
    117      1.110  christos #define XZ_CMD		"xz"		/* command to run as xz */
    118        1.6       mrg #define COMPRESS_CMD	"compress"	/* command to run as compress */
    119        1.6       mrg 
    120        1.1       jtc /*
    121       1.47  christos  * Long options.
    122       1.47  christos  */
    123       1.47  christos #define	OPT_USE_COMPRESS_PROGRAM	0
    124       1.47  christos #define	OPT_CHECKPOINT			1
    125       1.47  christos #define	OPT_UNLINK			2
    126       1.47  christos #define	OPT_HELP			3
    127       1.47  christos #define	OPT_ATIME_PRESERVE		4
    128       1.47  christos #define	OPT_IGNORE_FAILED_READ		5
    129       1.47  christos #define	OPT_REMOVE_FILES		6
    130       1.47  christos #define	OPT_NULL			7
    131       1.47  christos #define	OPT_TOTALS			8
    132       1.47  christos #define	OPT_VERSION			9
    133       1.47  christos #define	OPT_EXCLUDE			10
    134       1.47  christos #define	OPT_BLOCK_COMPRESS		11
    135       1.47  christos #define	OPT_NORECURSE			12
    136       1.47  christos #define	OPT_FORCE_LOCAL			13
    137       1.47  christos #define	OPT_INSECURE			14
    138       1.48  christos #define	OPT_STRICT			15
    139       1.78  christos #define	OPT_SPARSE			16
    140      1.110  christos #define OPT_XZ				17
    141      1.115  christos #define OPT_GNU				18
    142      1.117  christos #define	OPT_TIMESTAMP			19
    143       1.81       jmc #if !HAVE_NBTOOL_CONFIG_H
    144      1.117  christos #define	OPT_CHROOT			20
    145       1.81       jmc #endif
    146       1.47  christos 
    147       1.47  christos /*
    148        1.1       jtc  *	Format specific routine table - MUST BE IN SORTED ORDER BY NAME
    149        1.1       jtc  *	(see pax.h for description of each function)
    150        1.1       jtc  *
    151       1.26     itohy  *	name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
    152        1.1       jtc  *	read, end_read, st_write, write, end_write, trail,
    153       1.77  christos  *	subtrail, rd_data, wr_data, options
    154        1.1       jtc  */
    155        1.1       jtc 
    156        1.1       jtc FSUB fsub[] = {
    157       1.89  christos #ifndef NO_CPIO
    158        1.1       jtc /* 0: OLD BINARY CPIO */
    159        1.8  christos 	{ "bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
    160        1.8  christos 	bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, NULL,
    161        1.8  christos 	cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
    162        1.1       jtc 
    163        1.1       jtc /* 1: OLD OCTAL CHARACTER CPIO */
    164        1.8  christos 	{ "cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
    165        1.8  christos 	cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, NULL,
    166        1.8  christos 	cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
    167        1.1       jtc 
    168        1.1       jtc /* 2: SVR4 HEX CPIO */
    169        1.8  christos 	{ "sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
    170        1.8  christos 	vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, NULL,
    171        1.8  christos 	cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
    172        1.1       jtc 
    173        1.1       jtc /* 3: SVR4 HEX CPIO WITH CRC */
    174        1.8  christos 	{ "sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
    175        1.8  christos 	vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, NULL,
    176        1.8  christos 	cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
    177       1.89  christos #endif
    178        1.1       jtc /* 4: OLD TAR */
    179        1.8  christos 	{ "tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
    180        1.1       jtc 	tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
    181        1.8  christos 	NULL, rd_wrfile, wr_rdfile, tar_opt },
    182        1.1       jtc 
    183        1.1       jtc /* 5: POSIX USTAR */
    184        1.8  christos 	{ "ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
    185        1.1       jtc 	ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
    186        1.8  christos 	NULL, rd_wrfile, wr_rdfile, bad_opt }
    187        1.1       jtc };
    188       1.89  christos #ifndef NO_CPIO
    189       1.10       mrg #define F_BCPIO		0	/* old binary cpio format */
    190       1.10       mrg #define F_CPIO		1	/* old octal character cpio format */
    191       1.10       mrg #define F_SV4CPIO	2	/* SVR4 hex cpio format */
    192       1.10       mrg #define F_SV4CRC	3	/* SVR4 hex with crc cpio format */
    193       1.18        tv #define F_TAR		4	/* old V7 UNIX tar format */
    194       1.10       mrg #define F_USTAR		5	/* ustar format */
    195       1.89  christos #else
    196       1.89  christos #define F_TAR		0	/* old V7 UNIX tar format */
    197       1.89  christos #define F_USTAR		1	/* ustar format */
    198       1.89  christos #endif
    199       1.10       mrg #define DEFLT		F_USTAR	/* default write format from list above */
    200        1.1       jtc 
    201        1.1       jtc /*
    202        1.1       jtc  * ford is the archive search order used by get_arc() to determine what kind
    203       1.61       wiz  * of archive we are dealing with. This helps to properly id archive formats
    204        1.1       jtc  * some formats may be subsets of others....
    205        1.1       jtc  */
    206       1.89  christos int ford[] = {F_USTAR, F_TAR,
    207       1.89  christos #ifndef NO_CPIO
    208       1.89  christos     F_SV4CRC, F_SV4CPIO, F_CPIO, F_BCPIO,
    209       1.89  christos #endif
    210       1.89  christos     -1};
    211        1.1       jtc 
    212        1.1       jtc /*
    213       1.75  christos  * filename record separator
    214       1.75  christos  */
    215       1.75  christos int sep = '\n';
    216       1.75  christos 
    217       1.75  christos /*
    218       1.97  christos  * Do we have -C anywhere?
    219       1.97  christos  */
    220       1.97  christos int havechd = 0;
    221       1.97  christos 
    222       1.97  christos /*
    223        1.1       jtc  * options()
    224        1.1       jtc  *	figure out if we are pax, tar or cpio. Call the appropriate options
    225        1.1       jtc  *	parser
    226        1.1       jtc  */
    227        1.1       jtc 
    228        1.1       jtc void
    229        1.7       tls options(int argc, char **argv)
    230        1.1       jtc {
    231        1.1       jtc 
    232        1.1       jtc 	/*
    233        1.1       jtc 	 * Are we acting like pax, tar or cpio (based on argv[0])
    234        1.1       jtc 	 */
    235        1.1       jtc 	if ((argv0 = strrchr(argv[0], '/')) != NULL)
    236        1.1       jtc 		argv0++;
    237        1.1       jtc 	else
    238        1.1       jtc 		argv0 = argv[0];
    239        1.1       jtc 
    240       1.87  christos 	if (strstr(argv0, NM_TAR)) {
    241       1.87  christos 		argv0 = NM_TAR;
    242       1.56     grant 		tar_options(argc, argv);
    243       1.89  christos #ifndef NO_CPIO
    244       1.87  christos 	} else if (strstr(argv0, NM_CPIO)) {
    245       1.87  christos 		argv0 = NM_CPIO;
    246       1.56     grant 		cpio_options(argc, argv);
    247       1.89  christos #endif
    248       1.87  christos 	} else {
    249       1.13   mycroft 		argv0 = NM_PAX;
    250       1.56     grant 		pax_options(argc, argv);
    251       1.13   mycroft 	}
    252        1.1       jtc }
    253        1.1       jtc 
    254       1.47  christos struct option pax_longopts[] = {
    255       1.47  christos 	{ "insecure",		no_argument,		0,
    256       1.47  christos 						OPT_INSECURE },
    257       1.50  christos 	{ "force-local",	no_argument,		0,
    258       1.50  christos 						OPT_FORCE_LOCAL },
    259      1.107  christos 	{ "use-compress-program", required_argument,	0,
    260      1.107  christos 						OPT_USE_COMPRESS_PROGRAM },
    261      1.110  christos 	{ "xz",			no_argument,		0,
    262      1.110  christos 						OPT_XZ },
    263      1.115  christos 	{ "gnu",		no_argument,		0,
    264      1.115  christos 						OPT_GNU },
    265      1.118  christos 	{ "timestamp",		required_argument,	0,
    266      1.118  christos 						OPT_TIMESTAMP },
    267       1.98  christos 	{ 0,			0,			0,
    268       1.98  christos 						0 },
    269       1.47  christos };
    270       1.47  christos 
    271        1.1       jtc /*
    272        1.1       jtc  * pax_options()
    273        1.1       jtc  *	look at the user specified flags. set globals as required and check if
    274        1.1       jtc  *	the user specified a legal set of flags. If not, complain and exit
    275        1.1       jtc  */
    276        1.1       jtc 
    277        1.1       jtc static void
    278        1.7       tls pax_options(int argc, char **argv)
    279        1.1       jtc {
    280        1.7       tls 	int c;
    281      1.103     lukem 	size_t i;
    282      1.100  christos 	u_int64_t flg = 0;
    283      1.100  christos 	u_int64_t bflg = 0;
    284        1.7       tls 	char *pt;
    285       1.26     itohy 	FSUB tmp;
    286        1.1       jtc 
    287        1.1       jtc 	/*
    288        1.1       jtc 	 * process option flags
    289        1.1       jtc 	 */
    290       1.47  christos 	while ((c = getopt_long(argc, argv,
    291      1.100  christos 	    "0ab:cdf:ijklno:p:rs:tuvwx:zAB:DE:G:HLMN:OPT:U:VXYZ",
    292       1.47  christos 	    pax_longopts, NULL)) != -1) {
    293        1.1       jtc 		switch (c) {
    294       1.75  christos 		case '0':
    295       1.75  christos 			sep = '\0';
    296       1.75  christos 			break;
    297        1.1       jtc 		case 'a':
    298        1.1       jtc 			/*
    299        1.1       jtc 			 * append
    300        1.1       jtc 			 */
    301        1.1       jtc 			flg |= AF;
    302        1.1       jtc 			break;
    303        1.1       jtc 		case 'b':
    304        1.1       jtc 			/*
    305        1.1       jtc 			 * specify blocksize
    306        1.1       jtc 			 */
    307        1.1       jtc 			flg |= BF;
    308        1.1       jtc 			if ((wrblksz = (int)str_offt(optarg)) <= 0) {
    309        1.8  christos 				tty_warn(1, "Invalid block size %s", optarg);
    310        1.1       jtc 				pax_usage();
    311        1.1       jtc 			}
    312        1.1       jtc 			break;
    313        1.1       jtc 		case 'c':
    314        1.1       jtc 			/*
    315        1.1       jtc 			 * inverse match on patterns
    316        1.1       jtc 			 */
    317        1.1       jtc 			cflag = 1;
    318        1.1       jtc 			flg |= CF;
    319        1.1       jtc 			break;
    320        1.1       jtc 		case 'd':
    321        1.1       jtc 			/*
    322        1.1       jtc 			 * match only dir on extract, not the subtree at dir
    323        1.1       jtc 			 */
    324        1.1       jtc 			dflag = 1;
    325        1.1       jtc 			flg |= DF;
    326        1.1       jtc 			break;
    327        1.1       jtc 		case 'f':
    328        1.1       jtc 			/*
    329        1.1       jtc 			 * filename where the archive is stored
    330        1.1       jtc 			 */
    331        1.1       jtc 			arcname = optarg;
    332        1.1       jtc 			flg |= FF;
    333        1.1       jtc 			break;
    334        1.1       jtc 		case 'i':
    335        1.1       jtc 			/*
    336        1.1       jtc 			 * interactive file rename
    337        1.1       jtc 			 */
    338        1.1       jtc 			iflag = 1;
    339        1.1       jtc 			flg |= IF;
    340        1.1       jtc 			break;
    341       1.63  christos 		case 'j':
    342       1.63  christos 			/*
    343       1.63  christos 			 * pass through bzip2
    344       1.63  christos 			 */
    345       1.63  christos 			gzip_program = BZIP2_CMD;
    346       1.63  christos 			break;
    347        1.1       jtc 		case 'k':
    348        1.1       jtc 			/*
    349        1.1       jtc 			 * do not clobber files that exist
    350        1.1       jtc 			 */
    351        1.1       jtc 			kflag = 1;
    352        1.1       jtc 			flg |= KF;
    353        1.1       jtc 			break;
    354        1.1       jtc 		case 'l':
    355        1.1       jtc 			/*
    356        1.1       jtc 			 * try to link src to dest with copy (-rw)
    357        1.1       jtc 			 */
    358        1.1       jtc 			lflag = 1;
    359        1.1       jtc 			flg |= LF;
    360        1.1       jtc 			break;
    361        1.1       jtc 		case 'n':
    362        1.1       jtc 			/*
    363        1.1       jtc 			 * select first match for a pattern only
    364        1.1       jtc 			 */
    365        1.1       jtc 			nflag = 1;
    366        1.1       jtc 			flg |= NF;
    367        1.1       jtc 			break;
    368        1.1       jtc 		case 'o':
    369        1.1       jtc 			/*
    370        1.1       jtc 			 * pass format specific options
    371        1.1       jtc 			 */
    372        1.1       jtc 			flg |= OF;
    373        1.1       jtc 			if (opt_add(optarg) < 0)
    374        1.1       jtc 				pax_usage();
    375        1.1       jtc 			break;
    376        1.1       jtc 		case 'p':
    377        1.1       jtc 			/*
    378        1.1       jtc 			 * specify file characteristic options
    379        1.1       jtc 			 */
    380        1.1       jtc 			for (pt = optarg; *pt != '\0'; ++pt) {
    381        1.1       jtc 				switch(*pt) {
    382        1.1       jtc 				case 'a':
    383        1.1       jtc 					/*
    384        1.1       jtc 					 * do not preserve access time
    385        1.1       jtc 					 */
    386        1.1       jtc 					patime = 0;
    387        1.1       jtc 					break;
    388        1.1       jtc 				case 'e':
    389        1.1       jtc 					/*
    390        1.1       jtc 					 * preserve user id, group id, file
    391        1.1       jtc 					 * mode, access/modification times
    392       1.24       mrg 					 * and file flags.
    393        1.1       jtc 					 */
    394        1.1       jtc 					pids = 1;
    395        1.1       jtc 					pmode = 1;
    396        1.1       jtc 					patime = 1;
    397        1.1       jtc 					pmtime = 1;
    398       1.24       mrg 					pfflags = 1;
    399       1.24       mrg 					break;
    400       1.25   mycroft #if 0
    401       1.24       mrg 				case 'f':
    402       1.24       mrg 					/*
    403       1.24       mrg 					 * do not preserve file flags
    404       1.24       mrg 					 */
    405       1.24       mrg 					pfflags = 0;
    406        1.1       jtc 					break;
    407       1.25   mycroft #endif
    408        1.1       jtc 				case 'm':
    409        1.1       jtc 					/*
    410        1.1       jtc 					 * do not preserve modification time
    411        1.1       jtc 					 */
    412        1.1       jtc 					pmtime = 0;
    413        1.1       jtc 					break;
    414        1.1       jtc 				case 'o':
    415        1.1       jtc 					/*
    416        1.1       jtc 					 * preserve uid/gid
    417        1.1       jtc 					 */
    418        1.1       jtc 					pids = 1;
    419        1.1       jtc 					break;
    420        1.1       jtc 				case 'p':
    421        1.1       jtc 					/*
    422       1.35     lukem 					 * preserve file mode bits
    423        1.1       jtc 					 */
    424        1.1       jtc 					pmode = 1;
    425        1.1       jtc 					break;
    426        1.1       jtc 				default:
    427       1.40  christos 					tty_warn(1, "Invalid -p string: %c",
    428       1.40  christos 					    *pt);
    429        1.1       jtc 					pax_usage();
    430        1.1       jtc 					break;
    431        1.1       jtc 				}
    432        1.1       jtc 			}
    433        1.1       jtc 			flg |= PF;
    434        1.1       jtc 			break;
    435        1.1       jtc 		case 'r':
    436        1.1       jtc 			/*
    437        1.1       jtc 			 * read the archive
    438        1.1       jtc 			 */
    439        1.1       jtc 			flg |= RF;
    440        1.1       jtc 			break;
    441        1.1       jtc 		case 's':
    442        1.1       jtc 			/*
    443        1.1       jtc 			 * file name substitution name pattern
    444        1.1       jtc 			 */
    445        1.1       jtc 			if (rep_add(optarg) < 0) {
    446        1.1       jtc 				pax_usage();
    447        1.1       jtc 				break;
    448        1.1       jtc 			}
    449        1.1       jtc 			flg |= SF;
    450        1.1       jtc 			break;
    451        1.1       jtc 		case 't':
    452        1.1       jtc 			/*
    453        1.1       jtc 			 * preserve access time on filesystem nodes we read
    454        1.1       jtc 			 */
    455        1.1       jtc 			tflag = 1;
    456        1.1       jtc 			flg |= TF;
    457        1.1       jtc 			break;
    458        1.1       jtc 		case 'u':
    459        1.1       jtc 			/*
    460        1.1       jtc 			 * ignore those older files
    461        1.1       jtc 			 */
    462        1.1       jtc 			uflag = 1;
    463        1.1       jtc 			flg |= UF;
    464        1.1       jtc 			break;
    465        1.1       jtc 		case 'v':
    466        1.1       jtc 			/*
    467        1.1       jtc 			 * verbose operation mode
    468        1.1       jtc 			 */
    469        1.1       jtc 			vflag = 1;
    470        1.1       jtc 			flg |= VF;
    471        1.1       jtc 			break;
    472        1.1       jtc 		case 'w':
    473        1.1       jtc 			/*
    474        1.1       jtc 			 * write an archive
    475        1.1       jtc 			 */
    476        1.1       jtc 			flg |= WF;
    477        1.1       jtc 			break;
    478        1.1       jtc 		case 'x':
    479        1.1       jtc 			/*
    480        1.1       jtc 			 * specify an archive format on write
    481        1.1       jtc 			 */
    482        1.1       jtc 			tmp.name = optarg;
    483        1.8  christos 			frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
    484        1.8  christos 			    sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
    485        1.8  christos 			if (frmt != NULL) {
    486        1.1       jtc 				flg |= XF;
    487        1.1       jtc 				break;
    488        1.1       jtc 			}
    489        1.8  christos 			tty_warn(1, "Unknown -x format: %s", optarg);
    490        1.1       jtc 			(void)fputs("pax: Known -x formats are:", stderr);
    491        1.1       jtc 			for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
    492        1.1       jtc 				(void)fprintf(stderr, " %s", fsub[i].name);
    493        1.1       jtc 			(void)fputs("\n\n", stderr);
    494        1.1       jtc 			pax_usage();
    495        1.1       jtc 			break;
    496        1.6       mrg 		case 'z':
    497        1.6       mrg 			/*
    498        1.6       mrg 			 * use gzip.  Non standard option.
    499        1.6       mrg 			 */
    500        1.6       mrg 			gzip_program = GZIP_CMD;
    501       1.20      tron 			break;
    502       1.20      tron 		case 'A':
    503       1.20      tron 			Aflag = 1;
    504       1.20      tron 			flg |= CAF;
    505        1.6       mrg 			break;
    506        1.1       jtc 		case 'B':
    507        1.1       jtc 			/*
    508        1.1       jtc 			 * non-standard option on number of bytes written on a
    509        1.1       jtc 			 * single archive volume.
    510        1.1       jtc 			 */
    511        1.1       jtc 			if ((wrlimit = str_offt(optarg)) <= 0) {
    512        1.8  christos 				tty_warn(1, "Invalid write limit %s", optarg);
    513        1.1       jtc 				pax_usage();
    514        1.1       jtc 			}
    515        1.1       jtc 			if (wrlimit % BLKMULT) {
    516        1.8  christos 				tty_warn(1,
    517        1.8  christos 				    "Write limit is not a %d byte multiple",
    518        1.1       jtc 				    BLKMULT);
    519        1.1       jtc 				pax_usage();
    520        1.1       jtc 			}
    521        1.1       jtc 			flg |= CBF;
    522        1.1       jtc 			break;
    523        1.1       jtc 		case 'D':
    524        1.1       jtc 			/*
    525        1.1       jtc 			 * On extraction check file inode change time before the
    526        1.1       jtc 			 * modification of the file name. Non standard option.
    527        1.1       jtc 			 */
    528        1.1       jtc 			Dflag = 1;
    529        1.1       jtc 			flg |= CDF;
    530        1.1       jtc 			break;
    531        1.1       jtc 		case 'E':
    532        1.1       jtc 			/*
    533        1.1       jtc 			 * non-standard limit on read faults
    534        1.1       jtc 			 * 0 indicates stop after first error, values
    535       1.74  christos 			 * indicate a limit, "none" try forever
    536        1.1       jtc 			 */
    537        1.1       jtc 			flg |= CEF;
    538       1.74  christos 			if (strcmp(none, optarg) == 0)
    539        1.1       jtc 				maxflt = -1;
    540        1.1       jtc 			else if ((maxflt = atoi(optarg)) < 0) {
    541        1.8  christos 				tty_warn(1,
    542        1.8  christos 				    "Error count value must be positive");
    543        1.1       jtc 				pax_usage();
    544        1.1       jtc 			}
    545        1.1       jtc 			break;
    546        1.1       jtc 		case 'G':
    547        1.1       jtc 			/*
    548        1.1       jtc 			 * non-standard option for selecting files within an
    549        1.1       jtc 			 * archive by group (gid or name)
    550        1.1       jtc 			 */
    551        1.1       jtc 			if (grp_add(optarg) < 0) {
    552        1.1       jtc 				pax_usage();
    553        1.1       jtc 				break;
    554        1.1       jtc 			}
    555        1.1       jtc 			flg |= CGF;
    556        1.1       jtc 			break;
    557        1.1       jtc 		case 'H':
    558        1.1       jtc 			/*
    559        1.1       jtc 			 * follow command line symlinks only
    560        1.1       jtc 			 */
    561        1.1       jtc 			Hflag = 1;
    562        1.1       jtc 			flg |= CHF;
    563        1.1       jtc 			break;
    564        1.1       jtc 		case 'L':
    565        1.1       jtc 			/*
    566        1.1       jtc 			 * follow symlinks
    567        1.1       jtc 			 */
    568        1.1       jtc 			Lflag = 1;
    569        1.1       jtc 			flg |= CLF;
    570       1.27   thorpej 			break;
    571       1.39     lukem #ifdef SMALL
    572       1.39     lukem 		case 'M':
    573       1.39     lukem 		case 'N':
    574       1.39     lukem 			tty_warn(1, "Support for -%c is not compiled in", c);
    575       1.39     lukem 			exit(1);
    576       1.39     lukem #else	/* !SMALL */
    577       1.34     lukem 		case 'M':
    578       1.34     lukem 			/*
    579       1.34     lukem 			 * Treat list of filenames on stdin as an
    580       1.34     lukem 			 * mtree(8) specfile.  Non standard option.
    581       1.34     lukem 			 */
    582       1.34     lukem 			Mflag = 1;
    583       1.34     lukem 			flg |= CMF;
    584       1.34     lukem 			break;
    585       1.35     lukem 		case 'N':
    586       1.35     lukem 			/*
    587       1.35     lukem 			 * Use alternative directory for user db lookups.
    588       1.35     lukem 			 */
    589       1.36     lukem 			if (!setup_getid(optarg)) {
    590       1.36     lukem 				tty_warn(1,
    591       1.36     lukem 			    "Unable to use user and group databases in `%s'",
    592       1.36     lukem 				    optarg);
    593       1.35     lukem 				pax_usage();
    594       1.36     lukem 			}
    595       1.35     lukem 			break;
    596       1.39     lukem #endif	/* !SMALL */
    597       1.27   thorpej 		case 'O':
    598       1.27   thorpej 			/*
    599       1.27   thorpej 			 * Force one volume.  Non standard option.
    600       1.27   thorpej 			 */
    601       1.27   thorpej 			force_one_volume = 1;
    602        1.1       jtc 			break;
    603        1.1       jtc 		case 'P':
    604        1.1       jtc 			/*
    605        1.1       jtc 			 * do NOT follow symlinks (default)
    606        1.1       jtc 			 */
    607        1.1       jtc 			Lflag = 0;
    608        1.1       jtc 			flg |= CPF;
    609        1.1       jtc 			break;
    610        1.1       jtc 		case 'T':
    611        1.1       jtc 			/*
    612        1.1       jtc 			 * non-standard option for selecting files within an
    613        1.1       jtc 			 * archive by modification time range (lower,upper)
    614        1.1       jtc 			 */
    615        1.1       jtc 			if (trng_add(optarg) < 0) {
    616        1.1       jtc 				pax_usage();
    617        1.1       jtc 				break;
    618        1.1       jtc 			}
    619        1.1       jtc 			flg |= CTF;
    620        1.1       jtc 			break;
    621        1.1       jtc 		case 'U':
    622        1.1       jtc 			/*
    623        1.1       jtc 			 * non-standard option for selecting files within an
    624        1.1       jtc 			 * archive by user (uid or name)
    625        1.1       jtc 			 */
    626        1.1       jtc 			if (usr_add(optarg) < 0) {
    627        1.1       jtc 				pax_usage();
    628        1.1       jtc 				break;
    629        1.1       jtc 			}
    630        1.1       jtc 			flg |= CUF;
    631        1.1       jtc 			break;
    632      1.100  christos 		case 'V':
    633      1.100  christos 			/*
    634      1.100  christos 			 * somewhat verbose operation mode (no listing)
    635      1.100  christos 			 */
    636      1.100  christos 			Vflag = 1;
    637      1.100  christos 			flg |= VSF;
    638      1.100  christos 			break;
    639        1.1       jtc 		case 'X':
    640        1.1       jtc 			/*
    641        1.1       jtc 			 * do not pass over mount points in the file system
    642        1.1       jtc 			 */
    643        1.1       jtc 			Xflag = 1;
    644        1.1       jtc 			flg |= CXF;
    645        1.1       jtc 			break;
    646        1.1       jtc 		case 'Y':
    647        1.1       jtc 			/*
    648        1.1       jtc 			 * On extraction check file inode change time after the
    649        1.1       jtc 			 * modification of the file name. Non standard option.
    650        1.1       jtc 			 */
    651        1.1       jtc 			Yflag = 1;
    652        1.1       jtc 			flg |= CYF;
    653        1.1       jtc 			break;
    654        1.1       jtc 		case 'Z':
    655        1.1       jtc 			/*
    656        1.1       jtc 			 * On extraction check modification time after the
    657        1.1       jtc 			 * modification of the file name. Non standard option.
    658        1.1       jtc 			 */
    659        1.1       jtc 			Zflag = 1;
    660        1.1       jtc 			flg |= CZF;
    661        1.1       jtc 			break;
    662       1.47  christos 		case OPT_INSECURE:
    663       1.47  christos 			secure = 0;
    664       1.47  christos 			break;
    665       1.50  christos 		case OPT_FORCE_LOCAL:
    666      1.109     enami 			forcelocal = 1;
    667       1.50  christos 			break;
    668      1.107  christos 		case OPT_USE_COMPRESS_PROGRAM:
    669      1.107  christos 			gzip_program = optarg;
    670      1.107  christos 			break;
    671      1.110  christos 		case OPT_XZ:
    672      1.110  christos 			gzip_program = XZ_CMD;
    673      1.110  christos 			break;
    674      1.115  christos 		case OPT_GNU:
    675      1.115  christos 			is_gnutar = 1;
    676      1.115  christos 			break;
    677      1.118  christos #ifndef SMALL
    678      1.118  christos 		case OPT_TIMESTAMP:
    679      1.118  christos 			if (set_tstamp(optarg, &tst) == -1) {
    680      1.118  christos 				tty_warn(1, "Invalid timestamp `%s'", optarg);
    681      1.118  christos 				tar_usage();
    682      1.118  christos 			}
    683      1.118  christos 			break;
    684      1.118  christos #endif
    685        1.1       jtc 		case '?':
    686        1.1       jtc 		default:
    687        1.1       jtc 			pax_usage();
    688        1.1       jtc 			break;
    689        1.1       jtc 		}
    690        1.1       jtc 	}
    691        1.1       jtc 
    692        1.1       jtc 	/*
    693        1.1       jtc 	 * figure out the operation mode of pax read,write,extract,copy,append
    694        1.1       jtc 	 * or list. check that we have not been given a bogus set of flags
    695        1.1       jtc 	 * for the operation mode.
    696        1.1       jtc 	 */
    697        1.1       jtc 	if (ISLIST(flg)) {
    698        1.1       jtc 		act = LIST;
    699       1.40  christos 		listf = stdout;
    700        1.1       jtc 		bflg = flg & BDLIST;
    701        1.1       jtc 	} else if (ISEXTRACT(flg)) {
    702        1.1       jtc 		act = EXTRACT;
    703        1.1       jtc 		bflg = flg & BDEXTR;
    704        1.1       jtc 	} else if (ISARCHIVE(flg)) {
    705        1.1       jtc 		act = ARCHIVE;
    706        1.1       jtc 		bflg = flg & BDARCH;
    707        1.1       jtc 	} else if (ISAPPND(flg)) {
    708        1.1       jtc 		act = APPND;
    709        1.1       jtc 		bflg = flg & BDARCH;
    710        1.1       jtc 	} else if (ISCOPY(flg)) {
    711        1.1       jtc 		act = COPY;
    712        1.1       jtc 		bflg = flg & BDCOPY;
    713        1.1       jtc 	} else
    714        1.1       jtc 		pax_usage();
    715        1.1       jtc 	if (bflg) {
    716        1.1       jtc 		printflg(flg);
    717        1.1       jtc 		pax_usage();
    718        1.1       jtc 	}
    719        1.1       jtc 
    720        1.1       jtc 	/*
    721        1.1       jtc 	 * if we are writing (ARCHIVE) we use the default format if the user
    722        1.1       jtc 	 * did not specify a format. when we write during an APPEND, we will
    723        1.1       jtc 	 * adopt the format of the existing archive if none was supplied.
    724        1.1       jtc 	 */
    725        1.1       jtc 	if (!(flg & XF) && (act == ARCHIVE))
    726        1.1       jtc 		frmt = &(fsub[DEFLT]);
    727        1.1       jtc 
    728        1.1       jtc 	/*
    729        1.1       jtc 	 * process the args as they are interpreted by the operation mode
    730        1.1       jtc 	 */
    731        1.1       jtc 	switch (act) {
    732        1.1       jtc 	case LIST:
    733        1.1       jtc 	case EXTRACT:
    734        1.1       jtc 		for (; optind < argc; optind++)
    735      1.106  dholland 			if (pat_add(argv[optind], NULL, 0) < 0)
    736        1.1       jtc 				pax_usage();
    737        1.1       jtc 		break;
    738        1.1       jtc 	case COPY:
    739        1.1       jtc 		if (optind >= argc) {
    740        1.8  christos 			tty_warn(0, "Destination directory was not supplied");
    741        1.1       jtc 			pax_usage();
    742        1.1       jtc 		}
    743       1.42       mrg 		--argc;
    744       1.42       mrg 		dirptr = argv[argc];
    745       1.40  christos 		if (mkpath(dirptr) < 0)
    746       1.90  christos 			exit(1);
    747       1.14   mycroft 		/* FALLTHROUGH */
    748        1.1       jtc 	case ARCHIVE:
    749        1.1       jtc 	case APPND:
    750        1.1       jtc 		for (; optind < argc; optind++)
    751       1.23        is 			if (ftree_add(argv[optind], 0) < 0)
    752        1.1       jtc 				pax_usage();
    753        1.1       jtc 		/*
    754        1.1       jtc 		 * no read errors allowed on updates/append operation!
    755        1.1       jtc 		 */
    756        1.1       jtc 		maxflt = 0;
    757        1.1       jtc 		break;
    758        1.1       jtc 	}
    759        1.1       jtc }
    760        1.1       jtc 
    761        1.1       jtc 
    762        1.1       jtc /*
    763        1.1       jtc  * tar_options()
    764        1.1       jtc  *	look at the user specified flags. set globals as required and check if
    765        1.1       jtc  *	the user specified a legal set of flags. If not, complain and exit
    766        1.1       jtc  */
    767        1.1       jtc 
    768       1.29   thorpej struct option tar_longopts[] = {
    769       1.29   thorpej 	{ "block-size",		required_argument,	0,	'b' },
    770       1.62       wiz 	{ "bunzip2",		no_argument,		0,	'j' },
    771       1.62       wiz 	{ "bzip2",		no_argument,		0,	'j' },
    772       1.29   thorpej 	{ "create",		no_argument,		0,	'c' },	/* F */
    773       1.29   thorpej 	/* -e -- no corresponding long option */
    774       1.29   thorpej 	{ "file",		required_argument,	0,	'f' },
    775       1.29   thorpej 	{ "dereference",	no_argument,		0,	'h' },
    776       1.64  christos 	{ "keep-old-files",	no_argument,		0,	'k' },
    777       1.29   thorpej 	{ "one-file-system",	no_argument,		0,	'l' },
    778       1.29   thorpej 	{ "modification-time",	no_argument,		0,	'm' },
    779       1.29   thorpej 	{ "old-archive",	no_argument,		0,	'o' },
    780       1.29   thorpej 	{ "portability",	no_argument,		0,	'o' },
    781       1.29   thorpej 	{ "same-permissions",	no_argument,		0,	'p' },
    782       1.29   thorpej 	{ "preserve-permissions", no_argument,		0,	'p' },
    783       1.29   thorpej 	{ "preserve",		no_argument,		0,	'p' },
    784       1.46  christos 	{ "fast-read",		no_argument,		0,	'q' },
    785       1.29   thorpej 	{ "append",		no_argument,		0,	'r' },	/* F */
    786       1.29   thorpej 	{ "update",		no_argument,		0,	'u' },	/* F */
    787       1.29   thorpej 	{ "list",		no_argument,		0,	't' },	/* F */
    788       1.29   thorpej 	{ "verbose",		no_argument,		0,	'v' },
    789       1.29   thorpej 	{ "interactive",	no_argument,		0,	'w' },
    790       1.29   thorpej 	{ "confirmation",	no_argument,		0,	'w' },
    791       1.29   thorpej 	{ "extract",		no_argument,		0,	'x' },	/* F */
    792       1.29   thorpej 	{ "get",		no_argument,		0,	'x' },	/* F */
    793       1.29   thorpej 	{ "gzip",		no_argument,		0,	'z' },
    794       1.29   thorpej 	{ "gunzip",		no_argument,		0,	'z' },
    795       1.29   thorpej 	{ "read-full-blocks",	no_argument,		0,	'B' },
    796       1.29   thorpej 	{ "directory",		required_argument,	0,	'C' },
    797      1.116  christos 	{ "xz",			no_argument,		0,	'J' },
    798       1.65    simonb 	{ "to-stdout",		no_argument,		0,	'O' },
    799       1.29   thorpej 	{ "absolute-paths",	no_argument,		0,	'P' },
    800       1.80       dsl 	{ "sparse",		no_argument,		0,	'S' },
    801       1.40  christos 	{ "files-from",		required_argument,	0,	'T' },
    802      1.100  christos 	{ "summary",		no_argument,		0,	'V' },
    803      1.100  christos 	{ "stats",		no_argument,		0,	'V' },
    804       1.29   thorpej 	{ "exclude-from",	required_argument,	0,	'X' },
    805       1.29   thorpej 	{ "compress",		no_argument,		0,	'Z' },
    806       1.29   thorpej 	{ "uncompress",		no_argument,		0,	'Z' },
    807       1.48  christos 	{ "strict",		no_argument,		0,
    808       1.48  christos 						OPT_STRICT },
    809       1.31   thorpej 	{ "atime-preserve",	no_argument,		0,
    810       1.31   thorpej 						OPT_ATIME_PRESERVE },
    811       1.29   thorpej 	{ "unlink",		no_argument,		0,
    812       1.29   thorpej 						OPT_UNLINK },
    813       1.30   thorpej 	{ "use-compress-program", required_argument,	0,
    814       1.30   thorpej 						OPT_USE_COMPRESS_PROGRAM },
    815       1.40  christos 	{ "force-local",	no_argument,		0,
    816       1.40  christos 						OPT_FORCE_LOCAL },
    817       1.47  christos 	{ "insecure",		no_argument,		0,
    818       1.47  christos 						OPT_INSECURE },
    819       1.57       mrg 	{ "exclude",		required_argument,	0,
    820       1.57       mrg 						OPT_EXCLUDE },
    821       1.86  christos 	{ "no-recursion",	no_argument,		0,
    822       1.86  christos 						OPT_NORECURSE },
    823       1.81       jmc #if !HAVE_NBTOOL_CONFIG_H
    824       1.80       dsl 	{ "chroot",		no_argument,		0,
    825       1.80       dsl 						OPT_CHROOT },
    826       1.81       jmc #endif
    827      1.117  christos 	{ "timestamp",		required_argument,	0,
    828      1.117  christos 						OPT_TIMESTAMP },
    829       1.29   thorpej #if 0 /* Not implemented */
    830       1.29   thorpej 	{ "catenate",		no_argument,		0,	'A' },	/* F */
    831       1.29   thorpej 	{ "concatenate",	no_argument,		0,	'A' },	/* F */
    832       1.29   thorpej 	{ "diff",		no_argument,		0,	'd' },	/* F */
    833       1.29   thorpej 	{ "compare",		no_argument,		0,	'd' },	/* F */
    834       1.29   thorpej 	{ "checkpoint",		no_argument,		0,
    835       1.29   thorpej 						OPT_CHECKPOINT },
    836       1.31   thorpej 	{ "help",		no_argument,		0,
    837       1.29   thorpej 						OPT_HELP },
    838       1.29   thorpej 	{ "info-script",	required_argument,	0,	'F' },
    839       1.29   thorpej 	{ "new-volume-script",	required_argument,	0,	'F' },
    840       1.29   thorpej 	{ "incremental",	no_argument,		0,	'G' },
    841       1.29   thorpej 	{ "listed-incremental",	required_argument,	0,	'g' },
    842       1.29   thorpej 	{ "ignore-zeros",	no_argument,		0,	'i' },
    843       1.29   thorpej 	{ "ignore-failed-read",	no_argument,		0,
    844       1.29   thorpej 						OPT_IGNORE_FAILED_READ },
    845       1.29   thorpej 	{ "starting-file",	no_argument,		0,	'K' },
    846       1.50  christos 	{ "tape-length",	required_argument,	0,	'L' },
    847       1.29   thorpej 	{ "multi-volume",	no_argument,		0,	'M' },
    848       1.29   thorpej 	{ "after-date",		required_argument,	0,	'N' },
    849       1.29   thorpej 	{ "newer",		required_argument,	0,	'N' },
    850       1.29   thorpej 	{ "record-number",	no_argument,		0,	'R' },
    851       1.29   thorpej 	{ "remove-files",	no_argument,		0,
    852       1.29   thorpej 						OPT_REMOVE_FILES },
    853       1.29   thorpej 	{ "same-order",		no_argument,		0,	's' },
    854       1.29   thorpej 	{ "preserve-order",	no_argument,		0,	's' },
    855       1.29   thorpej 	{ "null",		no_argument,		0,
    856       1.29   thorpej 						OPT_NULL },
    857       1.29   thorpej 	{ "totals",		no_argument,		0,
    858       1.29   thorpej 						OPT_TOTALS },
    859      1.100  christos 	{ "volume-name",	required_argument,	0,	'V' }, /* XXX */
    860      1.100  christos 	{ "label",		required_argument,	0,	'V' }, /* XXX */
    861       1.29   thorpej 	{ "version",		no_argument,		0,
    862       1.29   thorpej 						OPT_VERSION },
    863       1.29   thorpej 	{ "verify",		no_argument,		0,	'W' },
    864       1.29   thorpej 	{ "block-compress",	no_argument,		0,
    865       1.29   thorpej 						OPT_BLOCK_COMPRESS },
    866       1.29   thorpej #endif
    867       1.29   thorpej 	{ 0,			0,			0,	0 },
    868       1.29   thorpej };
    869       1.29   thorpej 
    870        1.1       jtc static void
    871       1.99    cbiere tar_set_action(int op)
    872       1.99    cbiere {
    873       1.99    cbiere 	if (act != ERROR && act != op)
    874       1.99    cbiere 		tar_usage();
    875       1.99    cbiere 	act = op;
    876       1.99    cbiere }
    877       1.99    cbiere 
    878       1.99    cbiere static void
    879        1.7       tls tar_options(int argc, char **argv)
    880        1.1       jtc {
    881        1.7       tls 	int c;
    882        1.1       jtc 	int fstdin = 0;
    883       1.40  christos 	int Oflag = 0;
    884       1.40  christos 	int nincfiles = 0;
    885       1.40  christos 	int incfiles_max = 0;
    886       1.40  christos 	struct incfile {
    887       1.40  christos 		char *file;
    888       1.40  christos 		char *dir;
    889       1.40  christos 	};
    890       1.40  christos 	struct incfile *incfiles = NULL;
    891       1.40  christos 
    892       1.40  christos 	/*
    893       1.40  christos 	 * Set default values.
    894       1.40  christos 	 */
    895       1.40  christos 	rmleadslash = 1;
    896       1.49  christos 	is_gnutar = 1;
    897        1.1       jtc 
    898        1.1       jtc 	/*
    899        1.1       jtc 	 * process option flags
    900        1.1       jtc 	 */
    901       1.40  christos 	while ((c = getoldopt(argc, argv,
    902      1.116  christos 	    "+b:cef:hjklmopqrs:tuvwxzBC:HI:JOPST:X:Z014578",
    903       1.29   thorpej 	    tar_longopts, NULL))
    904        1.9     lukem 	    != -1)  {
    905        1.4       jtc 		switch(c) {
    906        1.1       jtc 		case 'b':
    907        1.1       jtc 			/*
    908       1.40  christos 			 * specify blocksize in 512-byte blocks
    909        1.1       jtc 			 */
    910        1.4       jtc 			if ((wrblksz = (int)str_offt(optarg)) <= 0) {
    911        1.8  christos 				tty_warn(1, "Invalid block size %s", optarg);
    912        1.1       jtc 				tar_usage();
    913        1.1       jtc 			}
    914       1.40  christos 			wrblksz *= 512;		/* XXX - check for int oflow */
    915        1.1       jtc 			break;
    916        1.1       jtc 		case 'c':
    917        1.1       jtc 			/*
    918        1.1       jtc 			 * create an archive
    919        1.1       jtc 			 */
    920       1.99    cbiere 			tar_set_action(ARCHIVE);
    921        1.1       jtc 			break;
    922        1.1       jtc 		case 'e':
    923        1.1       jtc 			/*
    924        1.1       jtc 			 * stop after first error
    925        1.1       jtc 			 */
    926        1.1       jtc 			maxflt = 0;
    927        1.1       jtc 			break;
    928        1.1       jtc 		case 'f':
    929        1.1       jtc 			/*
    930        1.1       jtc 			 * filename where the archive is stored
    931        1.1       jtc 			 */
    932        1.4       jtc 			if ((optarg[0] == '-') && (optarg[1]== '\0')) {
    933        1.1       jtc 				/*
    934        1.1       jtc 				 * treat a - as stdin
    935        1.1       jtc 				 */
    936        1.4       jtc 				fstdin = 1;
    937       1.40  christos 				arcname = NULL;
    938        1.1       jtc 				break;
    939        1.1       jtc 			}
    940        1.1       jtc 			fstdin = 0;
    941        1.4       jtc 			arcname = optarg;
    942        1.1       jtc 			break;
    943       1.21      tron 		case 'h':
    944       1.21      tron 			/*
    945       1.58  christos 			 * follow symlinks
    946       1.21      tron 			 */
    947       1.58  christos 			Lflag = 1;
    948       1.21      tron 			break;
    949       1.62       wiz 		case 'j':
    950       1.62       wiz 			/*
    951       1.62       wiz 			 * pass through bzip2. not a standard option
    952       1.62       wiz 			 */
    953       1.62       wiz 			gzip_program = BZIP2_CMD;
    954       1.64  christos 			break;
    955       1.64  christos 		case 'k':
    956       1.64  christos 			/*
    957       1.64  christos 			 * do not clobber files that exist
    958       1.64  christos 			 */
    959       1.64  christos 			kflag = 1;
    960       1.62       wiz 			break;
    961       1.10       mrg 		case 'l':
    962       1.10       mrg 			/*
    963       1.10       mrg 			 * do not pass over mount points in the file system
    964       1.10       mrg 			 */
    965       1.10       mrg 			Xflag = 1;
    966       1.10       mrg 			break;
    967        1.1       jtc 		case 'm':
    968        1.1       jtc 			/*
    969        1.1       jtc 			 * do not preserve modification time
    970        1.1       jtc 			 */
    971        1.1       jtc 			pmtime = 0;
    972        1.1       jtc 			break;
    973        1.1       jtc 		case 'o':
    974       1.18        tv 			/*
    975       1.18        tv 			 * This option does several things based on whether
    976       1.18        tv 			 * this is a create or extract operation.
    977       1.18        tv 			 */
    978       1.18        tv 			if (act == ARCHIVE) {
    979       1.48  christos 				/* GNU tar: write V7 format archives. */
    980       1.48  christos 				Oflag = 1;
    981       1.18        tv 				/* 4.2BSD: don't add directory entries. */
    982       1.18        tv 				if (opt_add("write_opt=nodir") < 0)
    983       1.18        tv 					tar_usage();
    984       1.18        tv 
    985       1.18        tv 			} else {
    986       1.18        tv 				/* SUS: don't preserve owner/group. */
    987       1.18        tv 				pids = 0;
    988       1.18        tv 				nopids = 1;
    989       1.18        tv 			}
    990        1.1       jtc 			break;
    991        1.1       jtc 		case 'p':
    992        1.1       jtc 			/*
    993        1.1       jtc 			 * preserve user id, group id, file
    994        1.1       jtc 			 * mode, access/modification times
    995        1.1       jtc 			 */
    996       1.18        tv 			if (!nopids)
    997       1.18        tv 				pids = 1;
    998        1.1       jtc 			pmode = 1;
    999        1.1       jtc 			patime = 1;
   1000        1.1       jtc 			pmtime = 1;
   1001        1.1       jtc 			break;
   1002       1.40  christos 		case 'q':
   1003       1.40  christos 			/*
   1004       1.40  christos 			 * select first match for a pattern only
   1005       1.40  christos 			 */
   1006       1.40  christos 			nflag = 1;
   1007       1.40  christos 			break;
   1008        1.1       jtc 		case 'r':
   1009        1.1       jtc 		case 'u':
   1010        1.1       jtc 			/*
   1011        1.1       jtc 			 * append to the archive
   1012        1.1       jtc 			 */
   1013       1.99    cbiere 			tar_set_action(APPND);
   1014        1.1       jtc 			break;
   1015       1.40  christos 		case 's':
   1016       1.40  christos 			/*
   1017       1.40  christos 			 * file name substitution name pattern
   1018       1.40  christos 			 */
   1019       1.40  christos 			if (rep_add(optarg) < 0) {
   1020       1.40  christos 				tar_usage();
   1021       1.40  christos 				break;
   1022       1.40  christos 			}
   1023       1.40  christos 			break;
   1024        1.1       jtc 		case 't':
   1025        1.1       jtc 			/*
   1026        1.1       jtc 			 * list contents of the tape
   1027        1.1       jtc 			 */
   1028       1.99    cbiere 			tar_set_action(LIST);
   1029        1.1       jtc 			break;
   1030        1.1       jtc 		case 'v':
   1031        1.1       jtc 			/*
   1032        1.1       jtc 			 * verbose operation mode
   1033        1.1       jtc 			 */
   1034        1.1       jtc 			vflag = 1;
   1035        1.1       jtc 			break;
   1036        1.1       jtc 		case 'w':
   1037        1.1       jtc 			/*
   1038        1.1       jtc 			 * interactive file rename
   1039        1.1       jtc 			 */
   1040        1.1       jtc 			iflag = 1;
   1041        1.1       jtc 			break;
   1042        1.1       jtc 		case 'x':
   1043        1.1       jtc 			/*
   1044       1.40  christos 			 * extract an archive, preserving mode,
   1045       1.40  christos 			 * and mtime if possible.
   1046        1.1       jtc 			 */
   1047       1.99    cbiere 			tar_set_action(EXTRACT);
   1048       1.40  christos 			pmtime = 1;
   1049        1.1       jtc 			break;
   1050        1.6       mrg 		case 'z':
   1051        1.6       mrg 			/*
   1052        1.6       mrg 			 * use gzip.  Non standard option.
   1053        1.6       mrg 			 */
   1054        1.6       mrg 			gzip_program = GZIP_CMD;
   1055        1.6       mrg 			break;
   1056        1.1       jtc 		case 'B':
   1057        1.1       jtc 			/*
   1058        1.1       jtc 			 * Nothing to do here, this is pax default
   1059        1.1       jtc 			 */
   1060        1.1       jtc 			break;
   1061       1.40  christos 		case 'C':
   1062       1.97  christos 			havechd++;
   1063       1.40  christos 			chdname = optarg;
   1064       1.40  christos 			break;
   1065       1.40  christos 		case 'H':
   1066       1.40  christos 			/*
   1067       1.40  christos 			 * follow command line symlinks only
   1068       1.40  christos 			 */
   1069       1.40  christos 			Hflag = 1;
   1070       1.40  christos 			break;
   1071       1.40  christos 		case 'I':
   1072       1.40  christos 		case 'T':
   1073       1.40  christos 			if (++nincfiles > incfiles_max) {
   1074       1.40  christos 				incfiles_max = nincfiles + 3;
   1075       1.40  christos 				incfiles = realloc(incfiles,
   1076       1.40  christos 				    sizeof(*incfiles) * incfiles_max);
   1077       1.40  christos 				if (incfiles == NULL) {
   1078       1.40  christos 					tty_warn(0, "Unable to allocate space "
   1079       1.40  christos 					    "for option list");
   1080       1.40  christos 					exit(1);
   1081       1.40  christos 				}
   1082       1.40  christos 			}
   1083       1.40  christos 			incfiles[nincfiles - 1].file = optarg;
   1084       1.40  christos 			incfiles[nincfiles - 1].dir = chdname;
   1085        1.1       jtc 			break;
   1086      1.116  christos 		case 'J':
   1087      1.116  christos 			gzip_program = XZ_CMD;
   1088      1.116  christos 			break;
   1089      1.116  christos 		case 'O':
   1090      1.116  christos 			Oflag = 1;
   1091      1.116  christos 			break;
   1092        1.1       jtc 		case 'P':
   1093       1.40  christos 			/*
   1094       1.40  christos 			 * do not remove leading '/' from pathnames
   1095       1.40  christos 			 */
   1096       1.40  christos 			rmleadslash = 0;
   1097       1.21      tron 			Aflag = 1;
   1098        1.1       jtc 			break;
   1099       1.78  christos 		case 'S':
   1100       1.78  christos 			/* do nothing; we already generate sparse files */
   1101       1.78  christos 			break;
   1102      1.100  christos 		case 'V':
   1103      1.100  christos 			/*
   1104      1.100  christos 			 * semi-verbose operation mode (no listing)
   1105      1.100  christos 			 */
   1106      1.100  christos 			Vflag = 1;
   1107      1.100  christos 			break;
   1108        1.1       jtc 		case 'X':
   1109        1.1       jtc 			/*
   1110       1.15       mrg 			 * GNU tar compat: exclude the files listed in optarg
   1111        1.1       jtc 			 */
   1112       1.15       mrg 			if (tar_gnutar_X_compat(optarg) != 0)
   1113       1.15       mrg 				tar_usage();
   1114        1.6       mrg 			break;
   1115        1.6       mrg 		case 'Z':
   1116        1.6       mrg 			/*
   1117        1.6       mrg 			 * use compress.
   1118        1.6       mrg 			 */
   1119        1.6       mrg 			gzip_program = COMPRESS_CMD;
   1120        1.1       jtc 			break;
   1121        1.1       jtc 		case '0':
   1122        1.1       jtc 			arcname = DEV_0;
   1123        1.1       jtc 			break;
   1124        1.1       jtc 		case '1':
   1125        1.1       jtc 			arcname = DEV_1;
   1126        1.1       jtc 			break;
   1127        1.1       jtc 		case '4':
   1128        1.1       jtc 			arcname = DEV_4;
   1129        1.1       jtc 			break;
   1130        1.1       jtc 		case '5':
   1131        1.1       jtc 			arcname = DEV_5;
   1132        1.1       jtc 			break;
   1133        1.1       jtc 		case '7':
   1134        1.1       jtc 			arcname = DEV_7;
   1135        1.1       jtc 			break;
   1136        1.1       jtc 		case '8':
   1137        1.1       jtc 			arcname = DEV_8;
   1138       1.31   thorpej 			break;
   1139       1.31   thorpej 		case OPT_ATIME_PRESERVE:
   1140       1.31   thorpej 			patime = 1;
   1141        1.1       jtc 			break;
   1142       1.29   thorpej 		case OPT_UNLINK:
   1143       1.29   thorpej 			/* Just ignore -- we always unlink first. */
   1144       1.30   thorpej 			break;
   1145       1.30   thorpej 		case OPT_USE_COMPRESS_PROGRAM:
   1146       1.30   thorpej 			gzip_program = optarg;
   1147       1.29   thorpej 			break;
   1148       1.40  christos 		case OPT_FORCE_LOCAL:
   1149       1.40  christos 			forcelocal = 1;
   1150       1.40  christos 			break;
   1151       1.47  christos 		case OPT_INSECURE:
   1152       1.47  christos 			secure = 0;
   1153       1.47  christos 			break;
   1154       1.48  christos 		case OPT_STRICT:
   1155       1.48  christos 			/* disable gnu extensions */
   1156       1.48  christos 			is_gnutar = 0;
   1157       1.57       mrg 			break;
   1158       1.57       mrg 		case OPT_EXCLUDE:
   1159       1.57       mrg 			if (tar_gnutar_minus_minus_exclude(optarg) != 0)
   1160       1.57       mrg 				tar_usage();
   1161       1.48  christos 			break;
   1162       1.86  christos 		case OPT_NORECURSE:
   1163       1.86  christos 			dflag = 1;
   1164       1.86  christos 			break;
   1165       1.81       jmc #if !HAVE_NBTOOL_CONFIG_H
   1166       1.80       dsl 		case OPT_CHROOT:
   1167       1.80       dsl 			do_chroot = 1;
   1168       1.80       dsl 			break;
   1169       1.81       jmc #endif
   1170      1.117  christos #ifndef SMALL
   1171      1.117  christos 		case OPT_TIMESTAMP:
   1172      1.117  christos 			if (set_tstamp(optarg, &tst) == -1) {
   1173      1.117  christos 				tty_warn(1, "Invalid timestamp `%s'", optarg);
   1174      1.117  christos 				tar_usage();
   1175      1.117  christos 			}
   1176      1.117  christos 			break;
   1177      1.117  christos #endif
   1178        1.1       jtc 		default:
   1179        1.1       jtc 			tar_usage();
   1180        1.1       jtc 			break;
   1181        1.1       jtc 		}
   1182        1.1       jtc 	}
   1183        1.4       jtc 	argc -= optind;
   1184        1.4       jtc 	argv += optind;
   1185       1.52     soren 
   1186       1.52     soren 	/* Tar requires an action. */
   1187       1.54  christos 	if (act == ERROR)
   1188       1.52     soren 		tar_usage();
   1189       1.17        tv 
   1190       1.40  christos 	/* Traditional tar behaviour (pax uses stderr unless in list mode) */
   1191       1.40  christos 	if (fstdin == 1 && act == ARCHIVE)
   1192       1.40  christos 		listf = stderr;
   1193       1.40  christos 	else
   1194       1.40  christos 		listf = stdout;
   1195       1.23        is 
   1196       1.40  christos 	/* Traditional tar behaviour (pax wants to read file list from stdin) */
   1197       1.40  christos 	if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
   1198       1.40  christos 		exit(0);
   1199       1.17        tv 	/*
   1200       1.17        tv 	 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
   1201       1.40  christos 	 * (unless -o specified)
   1202       1.17        tv 	 */
   1203       1.40  christos 	if (act == ARCHIVE || act == APPND)
   1204       1.40  christos 		frmt = &(fsub[Oflag ? F_TAR : F_USTAR]);
   1205       1.40  christos 	else if (Oflag) {
   1206       1.65    simonb 		if (act == EXTRACT)
   1207       1.65    simonb 			to_stdout = 1;
   1208       1.65    simonb 		else {
   1209       1.65    simonb 			tty_warn(1, "The -O/-o options are only valid when "
   1210       1.65    simonb 			    "writing or extracting an archive");
   1211       1.65    simonb 			tar_usage();
   1212       1.65    simonb 		}
   1213       1.40  christos 	}
   1214        1.1       jtc 
   1215        1.1       jtc 	/*
   1216        1.1       jtc 	 * process the args as they are interpreted by the operation mode
   1217        1.1       jtc 	 */
   1218        1.1       jtc 	switch (act) {
   1219        1.1       jtc 	case LIST:
   1220       1.40  christos 	case EXTRACT:
   1221        1.1       jtc 	default:
   1222       1.40  christos 		{
   1223       1.40  christos 			int sawpat = 0;
   1224       1.55  christos 			int dirisnext = 0;
   1225       1.95  christos 			char *file, *dir = NULL;
   1226      1.102  dholland 			int mustfreedir = 0;
   1227       1.40  christos 
   1228       1.40  christos 			while (nincfiles || *argv != NULL) {
   1229       1.40  christos 				/*
   1230       1.40  christos 				 * If we queued up any include files,
   1231       1.40  christos 				 * pull them in now.  Otherwise, check
   1232       1.40  christos 				 * for -I and -C positional flags.
   1233       1.40  christos 				 * Anything else must be a file to
   1234       1.40  christos 				 * extract.
   1235       1.40  christos 				 */
   1236       1.40  christos 				if (nincfiles) {
   1237       1.40  christos 					file = incfiles->file;
   1238       1.40  christos 					dir = incfiles->dir;
   1239      1.102  dholland 					mustfreedir = 0;
   1240       1.40  christos 					incfiles++;
   1241       1.40  christos 					nincfiles--;
   1242       1.40  christos 				} else if (strcmp(*argv, "-I") == 0) {
   1243       1.40  christos 					if (*++argv == NULL)
   1244       1.40  christos 						break;
   1245       1.40  christos 					file = *argv++;
   1246       1.40  christos 					dir = chdname;
   1247      1.102  dholland 					mustfreedir = 0;
   1248       1.88     lukem 				} else {
   1249       1.40  christos 					file = NULL;
   1250       1.88     lukem 					dir = NULL;
   1251      1.102  dholland 					mustfreedir = 0;
   1252       1.88     lukem 				}
   1253       1.40  christos 				if (file != NULL) {
   1254       1.40  christos 					FILE *fp;
   1255       1.40  christos 					char *str;
   1256       1.40  christos 
   1257       1.40  christos 					if (strcmp(file, "-") == 0)
   1258       1.40  christos 						fp = stdin;
   1259       1.40  christos 					else if ((fp = fopen(file, "r")) == NULL) {
   1260       1.40  christos 						tty_warn(1, "Unable to open file '%s' for read", file);
   1261       1.40  christos 						tar_usage();
   1262       1.40  christos 					}
   1263      1.105       roy 					while ((str = get_line(fp)) != NULL) {
   1264       1.55  christos 						if (dirisnext) {
   1265      1.102  dholland 							if (dir && mustfreedir)
   1266       1.95  christos 								free(dir);
   1267       1.55  christos 							dir = str;
   1268      1.102  dholland 							mustfreedir = 1;
   1269       1.55  christos 							dirisnext = 0;
   1270       1.55  christos 							continue;
   1271       1.55  christos 						}
   1272       1.55  christos 						if (strcmp(str, "-C") == 0) {
   1273       1.97  christos 							havechd++;
   1274       1.55  christos 							dirisnext = 1;
   1275       1.94  christos 							free(str);
   1276       1.55  christos 							continue;
   1277       1.55  christos 						}
   1278       1.79  christos 						if (strncmp(str, "-C ", 3) == 0) {
   1279       1.97  christos 							havechd++;
   1280      1.102  dholland 							if (dir && mustfreedir)
   1281       1.95  christos 								free(dir);
   1282       1.94  christos 							dir = strdup(str + 3);
   1283      1.102  dholland 							mustfreedir = 1;
   1284       1.94  christos 							free(str);
   1285       1.79  christos 							continue;
   1286       1.79  christos 						}
   1287      1.104     perry 						if (pat_add(str, dir, NOGLOB_MTCH) < 0)
   1288       1.40  christos 							tar_usage();
   1289       1.40  christos 						sawpat = 1;
   1290       1.40  christos 					}
   1291       1.55  christos 					/* Bomb if given -C w/out a dir. */
   1292       1.55  christos 					if (dirisnext)
   1293       1.55  christos 						tar_usage();
   1294      1.102  dholland 					if (dir && mustfreedir)
   1295       1.95  christos 						free(dir);
   1296       1.40  christos 					if (strcmp(file, "-") != 0)
   1297       1.40  christos 						fclose(fp);
   1298      1.105       roy 					if (get_line_error) {
   1299       1.40  christos 						tty_warn(1, "Problem with file '%s'", file);
   1300       1.40  christos 						tar_usage();
   1301       1.40  christos 					}
   1302       1.40  christos 				} else if (strcmp(*argv, "-C") == 0) {
   1303       1.40  christos 					if (*++argv == NULL)
   1304       1.40  christos  						break;
   1305       1.40  christos 					chdname = *argv++;
   1306       1.97  christos 					havechd++;
   1307      1.104     perry 				} else if (pat_add(*argv++, chdname, 0) < 0)
   1308       1.40  christos 					tar_usage();
   1309       1.40  christos 				else
   1310       1.40  christos 					sawpat = 1;
   1311       1.40  christos 			}
   1312       1.40  christos 			/*
   1313       1.40  christos 			 * if patterns were added, we are doing	chdir()
   1314       1.40  christos 			 * on a file-by-file basis, else, just one
   1315       1.40  christos 			 * global chdir (if any) after opening input.
   1316       1.40  christos 			 */
   1317       1.40  christos 			if (sawpat > 0)
   1318       1.40  christos 				chdname = NULL;
   1319       1.40  christos 		}
   1320       1.23        is 		break;
   1321        1.1       jtc 	case ARCHIVE:
   1322        1.1       jtc 	case APPND:
   1323       1.40  christos 		if (chdname != NULL) {	/* initial chdir() */
   1324       1.40  christos 			if (ftree_add(chdname, 1) < 0)
   1325       1.40  christos 				tar_usage();
   1326       1.40  christos 		}
   1327       1.40  christos 
   1328       1.40  christos 		while (nincfiles || *argv != NULL) {
   1329       1.40  christos 			char *file, *dir;
   1330       1.40  christos 
   1331       1.40  christos 			/*
   1332       1.40  christos 			 * If we queued up any include files, pull them in
   1333       1.40  christos 			 * now.  Otherwise, check for -I and -C positional
   1334       1.40  christos 			 * flags.  Anything else must be a file to include
   1335       1.40  christos 			 * in the archive.
   1336       1.40  christos 			 */
   1337       1.40  christos 			if (nincfiles) {
   1338       1.40  christos 				file = incfiles->file;
   1339       1.40  christos 				dir = incfiles->dir;
   1340       1.40  christos 				incfiles++;
   1341       1.40  christos 				nincfiles--;
   1342       1.40  christos 			} else if (strcmp(*argv, "-I") == 0) {
   1343       1.40  christos 				if (*++argv == NULL)
   1344       1.40  christos 					break;
   1345       1.40  christos 				file = *argv++;
   1346       1.40  christos 				dir = NULL;
   1347       1.88     lukem 			} else {
   1348       1.40  christos 				file = NULL;
   1349       1.88     lukem 				dir = NULL;
   1350       1.88     lukem 			}
   1351       1.40  christos 			if (file != NULL) {
   1352       1.40  christos 				FILE *fp;
   1353       1.40  christos 				char *str;
   1354       1.55  christos 				int dirisnext = 0;
   1355       1.40  christos 
   1356       1.40  christos 				/* Set directory if needed */
   1357       1.40  christos 				if (dir) {
   1358       1.40  christos 					if (ftree_add(dir, 1) < 0)
   1359       1.40  christos 						tar_usage();
   1360       1.40  christos 				}
   1361       1.40  christos 
   1362       1.40  christos 				if (strcmp(file, "-") == 0)
   1363       1.40  christos 					fp = stdin;
   1364       1.40  christos 				else if ((fp = fopen(file, "r")) == NULL) {
   1365       1.40  christos 					tty_warn(1, "Unable to open file '%s' for read", file);
   1366       1.40  christos 					tar_usage();
   1367       1.40  christos 				}
   1368      1.105       roy 				while ((str = get_line(fp)) != NULL) {
   1369       1.55  christos 					if (dirisnext) {
   1370       1.55  christos 						if (ftree_add(str, 1) < 0)
   1371       1.55  christos 							tar_usage();
   1372       1.55  christos 						dirisnext = 0;
   1373       1.55  christos 						continue;
   1374       1.55  christos 					}
   1375       1.55  christos 					if (strcmp(str, "-C") == 0) {
   1376       1.55  christos 						dirisnext = 1;
   1377       1.55  christos 						continue;
   1378       1.55  christos 					}
   1379       1.79  christos 					if (strncmp(str, "-C ", 3) == 0) {
   1380       1.79  christos 						if (ftree_add(str + 3, 1) < 0)
   1381       1.79  christos 							tar_usage();
   1382       1.79  christos 						continue;
   1383       1.79  christos 					}
   1384       1.40  christos 					if (ftree_add(str, 0) < 0)
   1385       1.40  christos 						tar_usage();
   1386       1.40  christos 				}
   1387       1.55  christos 				/* Bomb if given -C w/out a dir. */
   1388       1.55  christos 				if (dirisnext)
   1389       1.55  christos 					tar_usage();
   1390       1.40  christos 				if (strcmp(file, "-") != 0)
   1391       1.40  christos 					fclose(fp);
   1392      1.105       roy 				if (get_line_error) {
   1393       1.40  christos 					tty_warn(1, "Problem with file '%s'",
   1394       1.40  christos 					    file);
   1395       1.40  christos 					tar_usage();
   1396       1.40  christos 				}
   1397       1.40  christos 			} else if (strcmp(*argv, "-C") == 0) {
   1398       1.40  christos 				if (*++argv == NULL)
   1399       1.40  christos 					break;
   1400       1.40  christos 				if (ftree_add(*argv++, 1) < 0)
   1401       1.40  christos 					tar_usage();
   1402       1.40  christos 			} else if (ftree_add(*argv++, 0) < 0)
   1403       1.40  christos 				tar_usage();
   1404       1.40  christos 		}
   1405        1.1       jtc 		/*
   1406        1.1       jtc 		 * no read errors allowed on updates/append operation!
   1407        1.1       jtc 		 */
   1408        1.1       jtc 		maxflt = 0;
   1409        1.1       jtc 		break;
   1410        1.1       jtc 	}
   1411      1.112    plunky 	if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
   1412        1.1       jtc 		arcname = getenv("TAPE");
   1413       1.40  christos 		if ((arcname == NULL) || (*arcname == '\0'))
   1414       1.40  christos 			arcname = _PATH_DEFTAPE;
   1415       1.40  christos 	}
   1416       1.40  christos }
   1417       1.40  christos 
   1418       1.40  christos int
   1419      1.113      matt mkpath(char *path)
   1420       1.40  christos {
   1421       1.40  christos 	char *slash;
   1422       1.92  christos 	int done = 0;
   1423       1.40  christos 
   1424       1.40  christos 	slash = path;
   1425       1.40  christos 
   1426       1.40  christos 	while (!done) {
   1427       1.40  christos 		slash += strspn(slash, "/");
   1428       1.40  christos 		slash += strcspn(slash, "/");
   1429       1.40  christos 
   1430       1.40  christos 		done = (*slash == '\0');
   1431       1.40  christos 		*slash = '\0';
   1432       1.40  christos 
   1433       1.92  christos 		if (domkdir(path, 0777) == -1)
   1434       1.91  christos 			goto out;
   1435       1.40  christos 
   1436       1.40  christos 		if (!done)
   1437       1.40  christos 			*slash = '/';
   1438        1.1       jtc 	}
   1439       1.40  christos 
   1440       1.90  christos 	return 0;
   1441       1.91  christos out:
   1442       1.91  christos 	/* Can't create or or not a directory */
   1443       1.92  christos 	syswarn(1, errno, "Cannot create directory `%s'", path);
   1444       1.91  christos 	return -1;
   1445        1.1       jtc }
   1446        1.1       jtc 
   1447       1.45  christos 
   1448       1.89  christos #ifndef NO_CPIO
   1449       1.45  christos struct option cpio_longopts[] = {
   1450       1.45  christos 	{ "reset-access-time",	no_argument,		0,	'a' },
   1451       1.45  christos 	{ "make-directories",	no_argument,		0, 	'd' },
   1452       1.45  christos 	{ "nonmatching",	no_argument,		0,	'f' },
   1453       1.45  christos 	{ "extract",		no_argument,		0,	'i' },
   1454       1.45  christos 	{ "link",		no_argument,		0,	'l' },
   1455       1.45  christos 	{ "preserve-modification-time", no_argument,	0,	'm' },
   1456       1.45  christos 	{ "create",		no_argument,		0,	'o' },
   1457       1.45  christos 	{ "pass-through",	no_argument,		0,	'p' },
   1458       1.45  christos 	{ "rename",		no_argument,		0,	'r' },
   1459       1.45  christos 	{ "list",		no_argument,		0,	't' },
   1460       1.45  christos 	{ "unconditional",	no_argument,		0,	'u' },
   1461       1.45  christos 	{ "verbose",		no_argument,		0,	'v' },
   1462       1.45  christos 	{ "append",		no_argument,		0,	'A' },
   1463       1.45  christos 	{ "pattern-file",	required_argument,	0,	'E' },
   1464       1.45  christos 	{ "file",		required_argument,	0,	'F' },
   1465       1.45  christos 	{ "force-local",	no_argument,		0,
   1466       1.47  christos 						OPT_FORCE_LOCAL },
   1467       1.45  christos 	{ "format",		required_argument,	0,	'H' },
   1468       1.45  christos 	{ "dereference",	no_argument,		0,	'L' },
   1469       1.45  christos 	{ "swap-halfwords",	no_argument,		0,	'S' },
   1470      1.100  christos 	{ "summary",		no_argument,		0,	'V' },
   1471      1.100  christos 	{ "stats",		no_argument,		0,	'V' },
   1472       1.47  christos 	{ "insecure",		no_argument,		0,
   1473       1.47  christos 						OPT_INSECURE },
   1474       1.78  christos 	{ "sparse",		no_argument,		0,
   1475       1.78  christos 						OPT_SPARSE },
   1476      1.110  christos 	{ "xz",			no_argument,		0,
   1477      1.110  christos 						OPT_XZ },
   1478       1.45  christos 
   1479       1.45  christos #ifdef notyet
   1480       1.45  christos /* Not implemented */
   1481       1.45  christos 	{ "null",		no_argument,		0,	'0' },
   1482       1.45  christos 	{ "swap",		no_argument,		0,	'b' },
   1483       1.45  christos 	{ "numeric-uid-gid",	no_argument,		0,	'n' },
   1484       1.45  christos 	{ "swap-bytes",		no_argument,		0,	's' },
   1485       1.45  christos 	{ "message",		required_argument,	0,	'M' },
   1486       1.45  christos 	{ "owner",		required_argument,	0	'R' },
   1487      1.100  christos 	{ "dot",		no_argument,		0,	'V' }, /* xxx */
   1488       1.45  christos 	{ "block-size",		required_argument,	0,
   1489       1.47  christos 						OPT_BLOCK_SIZE },
   1490       1.45  christos 	{ "no-absolute-pathnames", no_argument,		0,
   1491       1.47  christos 						OPT_NO_ABSOLUTE_PATHNAMES },
   1492       1.45  christos 	{ "no-preserve-owner",	no_argument,		0,
   1493       1.47  christos 						OPT_NO_PRESERVE_OWNER },
   1494       1.45  christos 	{ "only-verify-crc",	no_argument,		0,
   1495       1.47  christos 						OPT_ONLY_VERIFY_CRC },
   1496       1.45  christos 	{ "rsh-command",	required_argument,	0,
   1497       1.47  christos 						OPT_RSH_COMMAND },
   1498       1.45  christos 	{ "version",		no_argument,		0,
   1499       1.47  christos 						OPT_VERSION },
   1500       1.45  christos #endif
   1501       1.83       jmc 	{ 0,			0,			0,	0 },
   1502       1.45  christos };
   1503       1.45  christos 
   1504       1.99    cbiere static void
   1505       1.99    cbiere cpio_set_action(int op)
   1506       1.99    cbiere {
   1507       1.99    cbiere 	if ((act == APPND && op == ARCHIVE) || (act == ARCHIVE && op == APPND))
   1508       1.99    cbiere 		act = APPND;
   1509      1.108       sjg 	else if (act == EXTRACT && op == LIST)
   1510      1.108       sjg 		act = op;
   1511       1.99    cbiere 	else if (act != ERROR && act != op)
   1512       1.99    cbiere 		cpio_usage();
   1513       1.99    cbiere 	else
   1514       1.99    cbiere 		act = op;
   1515       1.99    cbiere }
   1516       1.99    cbiere 
   1517        1.1       jtc /*
   1518        1.1       jtc  * cpio_options()
   1519        1.1       jtc  *	look at the user specified flags. set globals as required and check if
   1520        1.1       jtc  *	the user specified a legal set of flags. If not, complain and exit
   1521        1.1       jtc  */
   1522        1.1       jtc 
   1523        1.1       jtc static void
   1524        1.7       tls cpio_options(int argc, char **argv)
   1525        1.1       jtc {
   1526       1.26     itohy 	FSUB tmp;
   1527      1.100  christos 	u_int64_t flg = 0;
   1528      1.100  christos 	u_int64_t bflg = 0;
   1529      1.103     lukem 	int c;
   1530      1.103     lukem 	size_t i;
   1531       1.40  christos 	FILE *fp;
   1532       1.40  christos 	char *str;
   1533       1.10       mrg 
   1534       1.40  christos 	uflag = 1;
   1535       1.40  christos 	kflag = 1;
   1536       1.40  christos 	pids = 1;
   1537       1.40  christos 	pmode = 1;
   1538       1.40  christos 	pmtime = 0;
   1539       1.40  christos 	arcname = NULL;
   1540       1.40  christos 	dflag = 1;
   1541       1.40  christos 	nodirs = 1;
   1542       1.10       mrg 	/*
   1543       1.10       mrg 	 * process option flags
   1544       1.10       mrg 	 */
   1545       1.29   thorpej 	while ((c = getoldopt(argc, argv,
   1546       1.47  christos 	    "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
   1547       1.45  christos 	    cpio_longopts, NULL)) != -1) {
   1548       1.10       mrg 		switch(c) {
   1549       1.10       mrg 		case 'a':
   1550       1.10       mrg 			/*
   1551       1.10       mrg 			 * preserve access time on filesystem nodes we read
   1552       1.10       mrg 			 */
   1553       1.10       mrg 			tflag = 1;
   1554       1.10       mrg 			flg |= TF;
   1555       1.10       mrg 			break;
   1556       1.10       mrg #ifdef notyet
   1557       1.10       mrg 		case 'b':
   1558       1.40  christos 			/*
   1559       1.40  christos 			 * swap bytes and half-words when reading data
   1560       1.40  christos 			 */
   1561       1.10       mrg 			break;
   1562       1.10       mrg #endif
   1563       1.10       mrg 		case 'c':
   1564       1.40  christos 			/*
   1565       1.40  christos 			 * ASCII cpio header
   1566       1.40  christos 			 */
   1567       1.10       mrg 			frmt = &fsub[F_SV4CPIO];
   1568       1.10       mrg 			break;
   1569       1.10       mrg 		case 'd':
   1570       1.10       mrg 			/*
   1571       1.40  christos 			 * create directories as needed
   1572       1.10       mrg 			 * pax does this by default ..
   1573       1.10       mrg 			 */
   1574       1.40  christos 			nodirs = 0;
   1575       1.10       mrg 			break;
   1576       1.10       mrg 		case 'f':
   1577       1.10       mrg 			/*
   1578       1.10       mrg 			 * inverse match on patterns
   1579       1.10       mrg 			 */
   1580       1.10       mrg 			cflag = 1;
   1581       1.10       mrg 			flg |= CF;
   1582       1.10       mrg 			break;
   1583       1.10       mrg 		case 'i':
   1584       1.10       mrg 			/*
   1585       1.10       mrg 			 * read the archive
   1586       1.10       mrg 			 */
   1587       1.99    cbiere 			cpio_set_action(EXTRACT);
   1588       1.10       mrg 			flg |= RF;
   1589       1.10       mrg 			break;
   1590       1.10       mrg #ifdef notyet
   1591       1.10       mrg 		case 'k':
   1592       1.10       mrg 			break;
   1593       1.10       mrg #endif
   1594       1.10       mrg 		case 'l':
   1595       1.10       mrg 			/*
   1596       1.10       mrg 			 * try to link src to dest with copy (-rw)
   1597       1.10       mrg 			 */
   1598       1.10       mrg 			lflag = 1;
   1599       1.10       mrg 			flg |= LF;
   1600       1.10       mrg 			break;
   1601       1.10       mrg 		case 'm':
   1602       1.10       mrg 			/*
   1603       1.10       mrg 			 * preserve mtime
   1604       1.10       mrg 			 */
   1605       1.10       mrg 			flg |= PF;
   1606       1.10       mrg 			pmtime = 1;
   1607       1.10       mrg 			break;
   1608       1.10       mrg 		case 'o':
   1609       1.10       mrg 			/*
   1610       1.10       mrg 			 * write an archive
   1611       1.10       mrg 			 */
   1612       1.99    cbiere 			cpio_set_action(ARCHIVE);
   1613       1.40  christos 			frmt = &(fsub[F_SV4CRC]);
   1614       1.10       mrg 			flg |= WF;
   1615       1.10       mrg 			break;
   1616       1.10       mrg 		case 'p':
   1617       1.10       mrg 			/*
   1618       1.10       mrg 			 * cpio -p is like pax -rw
   1619       1.10       mrg 			 */
   1620       1.99    cbiere 			cpio_set_action(COPY);
   1621       1.10       mrg 			flg |= RF | WF;
   1622       1.10       mrg 			break;
   1623       1.10       mrg 		case 'r':
   1624       1.10       mrg 			/*
   1625       1.10       mrg 			 * interactive file rename
   1626       1.10       mrg 			 */
   1627       1.10       mrg 			iflag = 1;
   1628       1.10       mrg 			flg |= IF;
   1629       1.10       mrg 			break;
   1630       1.10       mrg #ifdef notyet
   1631       1.10       mrg 		case 's':
   1632       1.40  christos 			/*
   1633       1.40  christos 			 * swap bytes after reading data
   1634       1.40  christos 			 */
   1635       1.10       mrg 			break;
   1636       1.10       mrg #endif
   1637       1.10       mrg 		case 't':
   1638       1.40  christos 			/*
   1639       1.40  christos 			 * list contents of archive
   1640       1.40  christos 			 */
   1641       1.99    cbiere 			cpio_set_action(LIST);
   1642       1.40  christos 			listf = stdout;
   1643       1.82      tron 			flg &= ~RF;
   1644       1.10       mrg 			break;
   1645       1.10       mrg 		case 'u':
   1646       1.10       mrg 			/*
   1647       1.10       mrg 			 * don't ignore those older files
   1648       1.10       mrg 			 */
   1649       1.10       mrg 			uflag = 0;
   1650       1.40  christos 			kflag = 0;
   1651       1.10       mrg 			flg |= UF;
   1652       1.10       mrg 			break;
   1653       1.10       mrg 		case 'v':
   1654       1.10       mrg 			/*
   1655       1.10       mrg 			 * verbose operation mode
   1656       1.10       mrg 			 */
   1657       1.10       mrg 			vflag = 1;
   1658       1.10       mrg 			flg |= VF;
   1659       1.10       mrg 			break;
   1660       1.40  christos 		case 'z':
   1661       1.40  christos 			/*
   1662       1.40  christos 			 * use gzip.  Non standard option.
   1663       1.40  christos 			 */
   1664       1.40  christos 			gzip_program = GZIP_CMD;
   1665       1.40  christos 			break;
   1666       1.40  christos 		case 'A':
   1667       1.40  christos 			/*
   1668       1.40  christos 			 * append to an archive
   1669       1.40  christos 			 */
   1670       1.99    cbiere 			cpio_set_action(APPND);
   1671       1.40  christos 			flg |= AF;
   1672       1.40  christos 			break;
   1673       1.40  christos 		case 'B':
   1674       1.40  christos 			/*
   1675       1.40  christos 			 * set blocksize to 5120
   1676       1.40  christos 			 */
   1677       1.40  christos 			blksz = 5120;
   1678       1.40  christos 			break;
   1679       1.40  christos 		case 'C':
   1680       1.40  christos 			/*
   1681       1.40  christos 			 * specify blocksize
   1682       1.40  christos 			 */
   1683       1.40  christos 			if ((blksz = (int)str_offt(optarg)) <= 0) {
   1684       1.40  christos 				tty_warn(1, "Invalid block size %s", optarg);
   1685       1.59  kristerw 				cpio_usage();
   1686       1.40  christos 			}
   1687       1.40  christos 			break;
   1688       1.40  christos 		case 'E':
   1689       1.40  christos 			/*
   1690       1.40  christos 			 * file with patterns to extract or list
   1691       1.40  christos 			 */
   1692       1.40  christos 			if ((fp = fopen(optarg, "r")) == NULL) {
   1693       1.40  christos 				tty_warn(1, "Unable to open file '%s' for read",
   1694       1.40  christos 				    optarg);
   1695       1.40  christos 				cpio_usage();
   1696       1.40  christos 			}
   1697      1.105       roy 			while ((str = get_line(fp)) != NULL) {
   1698      1.104     perry 				pat_add(str, NULL, 0);
   1699       1.40  christos 			}
   1700       1.40  christos 			fclose(fp);
   1701      1.105       roy 			if (get_line_error) {
   1702       1.40  christos 				tty_warn(1, "Problem with file '%s'", optarg);
   1703       1.40  christos 				cpio_usage();
   1704       1.40  christos 			}
   1705       1.40  christos 			break;
   1706       1.40  christos 		case 'H':
   1707       1.40  christos 			/*
   1708       1.40  christos 			 * specify an archive format on write
   1709       1.40  christos 			 */
   1710       1.40  christos 			tmp.name = optarg;
   1711       1.40  christos 			frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
   1712       1.40  christos 			    sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
   1713       1.40  christos 			if (frmt != NULL) {
   1714       1.40  christos 				flg |= XF;
   1715       1.40  christos 				break;
   1716       1.40  christos 			}
   1717       1.40  christos 			tty_warn(1, "Unknown -H format: %s", optarg);
   1718       1.40  christos 			(void)fputs("cpio: Known -H formats are:", stderr);
   1719       1.40  christos 			for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
   1720       1.40  christos 				(void)fprintf(stderr, " %s", fsub[i].name);
   1721       1.40  christos 			(void)fputs("\n\n", stderr);
   1722       1.59  kristerw 			cpio_usage();
   1723       1.40  christos 			break;
   1724      1.109     enami 		case 'F':
   1725       1.40  christos 		case 'I':
   1726       1.40  christos 		case 'O':
   1727       1.40  christos 			/*
   1728       1.40  christos 			 * filename where the archive is stored
   1729       1.40  christos 			 */
   1730       1.40  christos 			if ((optarg[0] == '-') && (optarg[1]== '\0')) {
   1731       1.40  christos 				/*
   1732       1.40  christos 				 * treat a - as stdin
   1733       1.40  christos 				 */
   1734       1.40  christos 				arcname = NULL;
   1735       1.40  christos 				break;
   1736       1.40  christos 			}
   1737       1.40  christos 			arcname = optarg;
   1738       1.40  christos 			break;
   1739       1.40  christos 		case 'L':
   1740       1.40  christos 			/*
   1741       1.40  christos 			 * follow symlinks
   1742       1.40  christos 			 */
   1743       1.40  christos 			Lflag = 1;
   1744       1.40  christos 			flg |= CLF;
   1745       1.40  christos 			break;
   1746       1.40  christos #ifdef notyet
   1747       1.40  christos 		case 'M':
   1748       1.40  christos 			arg = optarg;
   1749       1.40  christos 			break;
   1750       1.40  christos 		case 'R':
   1751       1.40  christos 			arg = optarg;
   1752       1.40  christos 			break;
   1753       1.40  christos #endif
   1754       1.40  christos 		case 'S':
   1755       1.40  christos 			/*
   1756       1.40  christos 			 * swap halfwords after reading data
   1757       1.40  christos 			 */
   1758       1.40  christos 			cpio_swp_head = 1;
   1759       1.40  christos 			break;
   1760       1.40  christos #ifdef notyet
   1761      1.100  christos 		case 'V':		/* print a '.' for each file processed */
   1762      1.100  christos 			break;
   1763      1.100  christos #endif
   1764       1.40  christos 		case 'V':
   1765      1.100  christos 			/*
   1766      1.100  christos 			 * semi-verbose operation mode (no listing)
   1767      1.100  christos 			 */
   1768      1.100  christos 			Vflag = 1;
   1769      1.100  christos 			flg |= VF;
   1770       1.40  christos 			break;
   1771       1.40  christos 		case 'Z':
   1772       1.40  christos 			/*
   1773       1.40  christos 			 * use compress.  Non standard option.
   1774       1.40  christos 			 */
   1775       1.40  christos 			gzip_program = COMPRESS_CMD;
   1776       1.40  christos 			break;
   1777       1.40  christos 		case '6':
   1778       1.40  christos 			/*
   1779       1.40  christos 			 * process Version 6 cpio format
   1780       1.40  christos 			 */
   1781       1.40  christos 			frmt = &(fsub[F_BCPIO]);
   1782      1.109     enami 			break;
   1783       1.45  christos 		case OPT_FORCE_LOCAL:
   1784       1.45  christos 			forcelocal = 1;
   1785       1.47  christos 			break;
   1786       1.47  christos 		case OPT_INSECURE:
   1787       1.47  christos 			secure = 0;
   1788       1.45  christos 			break;
   1789       1.78  christos 		case OPT_SPARSE:
   1790       1.78  christos 			/* do nothing; we already generate sparse files */
   1791       1.78  christos 			break;
   1792      1.110  christos 		case OPT_XZ:
   1793      1.110  christos 			gzip_program = XZ_CMD;
   1794      1.110  christos 			break;
   1795       1.10       mrg 		default:
   1796       1.10       mrg 			cpio_usage();
   1797       1.10       mrg 			break;
   1798       1.10       mrg 		}
   1799       1.10       mrg 	}
   1800       1.10       mrg 
   1801       1.10       mrg 	/*
   1802       1.10       mrg 	 * figure out the operation mode of cpio. check that we have not been
   1803       1.10       mrg 	 * given a bogus set of flags for the operation mode.
   1804       1.10       mrg 	 */
   1805       1.10       mrg 	if (ISLIST(flg)) {
   1806       1.10       mrg 		act = LIST;
   1807       1.10       mrg 		bflg = flg & BDLIST;
   1808       1.10       mrg 	} else if (ISEXTRACT(flg)) {
   1809       1.10       mrg 		act = EXTRACT;
   1810       1.10       mrg 		bflg = flg & BDEXTR;
   1811       1.10       mrg 	} else if (ISARCHIVE(flg)) {
   1812       1.10       mrg 		act = ARCHIVE;
   1813       1.10       mrg 		bflg = flg & BDARCH;
   1814       1.10       mrg 	} else if (ISAPPND(flg)) {
   1815       1.10       mrg 		act = APPND;
   1816       1.10       mrg 		bflg = flg & BDARCH;
   1817       1.10       mrg 	} else if (ISCOPY(flg)) {
   1818       1.10       mrg 		act = COPY;
   1819       1.10       mrg 		bflg = flg & BDCOPY;
   1820       1.10       mrg 	} else
   1821       1.10       mrg 		cpio_usage();
   1822       1.10       mrg 	if (bflg) {
   1823       1.10       mrg 		cpio_usage();
   1824       1.10       mrg 	}
   1825       1.10       mrg 
   1826       1.10       mrg 	/*
   1827       1.10       mrg 	 * if we are writing (ARCHIVE) we use the default format if the user
   1828       1.10       mrg 	 * did not specify a format. when we write during an APPEND, we will
   1829       1.10       mrg 	 * adopt the format of the existing archive if none was supplied.
   1830       1.10       mrg 	 */
   1831       1.10       mrg 	if (!(flg & XF) && (act == ARCHIVE))
   1832       1.10       mrg 		frmt = &(fsub[F_BCPIO]);
   1833       1.10       mrg 
   1834       1.10       mrg 	/*
   1835       1.10       mrg 	 * process the args as they are interpreted by the operation mode
   1836       1.10       mrg 	 */
   1837       1.10       mrg 	switch (act) {
   1838       1.10       mrg 	case LIST:
   1839       1.10       mrg 	case EXTRACT:
   1840       1.10       mrg 		for (; optind < argc; optind++)
   1841      1.104     perry 			if (pat_add(argv[optind], NULL, 0) < 0)
   1842       1.10       mrg 				cpio_usage();
   1843       1.10       mrg 		break;
   1844       1.10       mrg 	case COPY:
   1845       1.10       mrg 		if (optind >= argc) {
   1846       1.10       mrg 			tty_warn(0, "Destination directory was not supplied");
   1847       1.10       mrg 			cpio_usage();
   1848       1.10       mrg 		}
   1849       1.10       mrg 		--argc;
   1850       1.10       mrg 		dirptr = argv[argc];
   1851       1.14   mycroft 		/* FALLTHROUGH */
   1852       1.10       mrg 	case ARCHIVE:
   1853       1.10       mrg 	case APPND:
   1854       1.40  christos 		if (argc != optind) {
   1855       1.40  christos 			for (; optind < argc; optind++)
   1856       1.40  christos 				if (ftree_add(argv[optind], 0) < 0)
   1857       1.40  christos 					cpio_usage();
   1858       1.40  christos 			break;
   1859       1.40  christos 		}
   1860       1.10       mrg 		/*
   1861       1.10       mrg 		 * no read errors allowed on updates/append operation!
   1862       1.10       mrg 		 */
   1863       1.10       mrg 		maxflt = 0;
   1864      1.105       roy 		while ((str = get_line(stdin)) != NULL) {
   1865       1.68      fvdl 			ftree_add(str, 0);
   1866       1.40  christos 		}
   1867      1.105       roy 		if (get_line_error) {
   1868       1.40  christos 			tty_warn(1, "Problem while reading stdin");
   1869       1.40  christos 			cpio_usage();
   1870       1.40  christos 		}
   1871       1.40  christos 		break;
   1872       1.40  christos 	default:
   1873       1.40  christos 		cpio_usage();
   1874       1.10       mrg 		break;
   1875       1.10       mrg 	}
   1876        1.1       jtc }
   1877       1.89  christos #endif
   1878        1.1       jtc 
   1879        1.1       jtc /*
   1880        1.1       jtc  * printflg()
   1881        1.1       jtc  *	print out those invalid flag sets found to the user
   1882        1.1       jtc  */
   1883        1.1       jtc 
   1884        1.1       jtc static void
   1885        1.1       jtc printflg(unsigned int flg)
   1886        1.1       jtc {
   1887        1.1       jtc 	int nxt;
   1888        1.1       jtc 
   1889        1.1       jtc 	(void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
   1890        1.8  christos 	while ((nxt = ffs(flg)) != 0) {
   1891       1.70      jmmv 		flg &= ~(1 << (nxt - 1));
   1892       1.70      jmmv 		(void)fprintf(stderr, " -%c", flgch[nxt - 1]);
   1893        1.1       jtc 	}
   1894        1.1       jtc 	(void)putc('\n', stderr);
   1895        1.1       jtc }
   1896        1.1       jtc 
   1897        1.1       jtc /*
   1898        1.1       jtc  * c_frmt()
   1899        1.1       jtc  *	comparison routine used by bsearch to find the format specified
   1900        1.1       jtc  *	by the user
   1901        1.1       jtc  */
   1902        1.1       jtc 
   1903        1.1       jtc static int
   1904        1.1       jtc c_frmt(const void *a, const void *b)
   1905        1.1       jtc {
   1906       1.93       dsl 	return strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name);
   1907        1.1       jtc }
   1908        1.1       jtc 
   1909        1.1       jtc /*
   1910        1.1       jtc  * opt_next()
   1911        1.1       jtc  *	called by format specific options routines to get each format specific
   1912        1.1       jtc  *	flag and value specified with -o
   1913        1.1       jtc  * Return:
   1914        1.1       jtc  *	pointer to next OPLIST entry or NULL (end of list).
   1915        1.1       jtc  */
   1916        1.1       jtc 
   1917        1.1       jtc OPLIST *
   1918        1.1       jtc opt_next(void)
   1919        1.1       jtc {
   1920        1.1       jtc 	OPLIST *opt;
   1921        1.1       jtc 
   1922        1.1       jtc 	if ((opt = ophead) != NULL)
   1923        1.1       jtc 		ophead = ophead->fow;
   1924       1.93       dsl 	return opt;
   1925        1.1       jtc }
   1926        1.1       jtc 
   1927        1.1       jtc /*
   1928        1.1       jtc  * bad_opt()
   1929        1.1       jtc  *	generic routine used to complain about a format specific options
   1930        1.1       jtc  *	when the format does not support options.
   1931        1.1       jtc  */
   1932        1.1       jtc 
   1933        1.1       jtc int
   1934        1.1       jtc bad_opt(void)
   1935        1.1       jtc {
   1936        1.7       tls 	OPLIST *opt;
   1937        1.1       jtc 
   1938        1.1       jtc 	if (ophead == NULL)
   1939       1.93       dsl 		return 0;
   1940        1.1       jtc 	/*
   1941        1.1       jtc 	 * print all we were given
   1942        1.1       jtc 	 */
   1943       1.48  christos 	tty_warn(1," These format options are not supported for %s",
   1944       1.48  christos 	    frmt->name);
   1945        1.1       jtc 	while ((opt = opt_next()) != NULL)
   1946        1.1       jtc 		(void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
   1947       1.48  christos 	if (strcmp(NM_TAR, argv0) == 0)
   1948       1.48  christos 		tar_usage();
   1949       1.89  christos #ifndef NO_CPIO
   1950       1.48  christos 	else if (strcmp(NM_CPIO, argv0) == 0)
   1951       1.48  christos 		cpio_usage();
   1952       1.89  christos #endif
   1953       1.48  christos 	else
   1954       1.48  christos 		pax_usage();
   1955       1.93       dsl 	return 0;
   1956        1.1       jtc }
   1957        1.1       jtc 
   1958        1.1       jtc /*
   1959        1.1       jtc  * opt_add()
   1960        1.1       jtc  *	breaks the value supplied to -o into a option name and value. options
   1961        1.1       jtc  *	are given to -o in the form -o name-value,name=value
   1962       1.26     itohy  *	multiple -o may be specified.
   1963        1.1       jtc  * Return:
   1964        1.1       jtc  *	0 if format in name=value format, -1 if -o is passed junk
   1965        1.1       jtc  */
   1966        1.1       jtc 
   1967        1.1       jtc int
   1968       1.18        tv opt_add(const char *str)
   1969        1.1       jtc {
   1970        1.7       tls 	OPLIST *opt;
   1971        1.7       tls 	char *frpt;
   1972        1.7       tls 	char *pt;
   1973        1.7       tls 	char *endpt;
   1974       1.40  christos 	char *dstr;
   1975        1.1       jtc 
   1976        1.1       jtc 	if ((str == NULL) || (*str == '\0')) {
   1977        1.8  christos 		tty_warn(0, "Invalid option name");
   1978       1.93       dsl 		return -1;
   1979        1.1       jtc 	}
   1980       1.40  christos 	if ((dstr = strdup(str)) == NULL) {
   1981       1.40  christos 		tty_warn(0, "Unable to allocate space for option list");
   1982       1.93       dsl 		return -1;
   1983       1.40  christos 	}
   1984       1.40  christos 	frpt = endpt = dstr;
   1985        1.1       jtc 
   1986        1.1       jtc 	/*
   1987        1.1       jtc 	 * break into name and values pieces and stuff each one into a
   1988        1.1       jtc 	 * OPLIST structure. When we know the format, the format specific
   1989        1.1       jtc 	 * option function will go through this list
   1990        1.1       jtc 	 */
   1991        1.1       jtc 	while ((frpt != NULL) && (*frpt != '\0')) {
   1992        1.1       jtc 		if ((endpt = strchr(frpt, ',')) != NULL)
   1993        1.1       jtc 			*endpt = '\0';
   1994        1.1       jtc 		if ((pt = strchr(frpt, '=')) == NULL) {
   1995        1.8  christos 			tty_warn(0, "Invalid options format");
   1996       1.40  christos 			free(dstr);
   1997       1.93       dsl 			return -1;
   1998        1.1       jtc 		}
   1999        1.1       jtc 		if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
   2000        1.8  christos 			tty_warn(0, "Unable to allocate space for option list");
   2001       1.40  christos 			free(dstr);
   2002       1.93       dsl 			return -1;
   2003        1.1       jtc 		}
   2004        1.1       jtc 		*pt++ = '\0';
   2005        1.1       jtc 		opt->name = frpt;
   2006        1.1       jtc 		opt->value = pt;
   2007        1.1       jtc 		opt->fow = NULL;
   2008        1.1       jtc 		if (endpt != NULL)
   2009        1.1       jtc 			frpt = endpt + 1;
   2010        1.1       jtc 		else
   2011        1.1       jtc 			frpt = NULL;
   2012        1.1       jtc 		if (ophead == NULL) {
   2013        1.1       jtc 			optail = ophead = opt;
   2014        1.1       jtc 			continue;
   2015        1.1       jtc 		}
   2016        1.1       jtc 		optail->fow = opt;
   2017        1.1       jtc 		optail = opt;
   2018        1.1       jtc 	}
   2019       1.93       dsl 	return 0;
   2020        1.1       jtc }
   2021        1.1       jtc 
   2022        1.1       jtc /*
   2023        1.1       jtc  * str_offt()
   2024        1.1       jtc  *	Convert an expression of the following forms to an off_t > 0.
   2025       1.26     itohy  *	1) A positive decimal number.
   2026        1.1       jtc  *	2) A positive decimal number followed by a b (mult by 512).
   2027        1.1       jtc  *	3) A positive decimal number followed by a k (mult by 1024).
   2028        1.1       jtc  *	4) A positive decimal number followed by a m (mult by 512).
   2029        1.1       jtc  *	5) A positive decimal number followed by a w (mult by sizeof int)
   2030        1.1       jtc  *	6) Two or more positive decimal numbers (with/without k,b or w).
   2031       1.32       wiz  *	   separated by x (also * for backwards compatibility), specifying
   2032        1.1       jtc  *	   the product of the indicated values.
   2033        1.1       jtc  * Return:
   2034        1.1       jtc  *	0 for an error, a positive value o.w.
   2035        1.1       jtc  */
   2036        1.1       jtc 
   2037        1.1       jtc static off_t
   2038        1.1       jtc str_offt(char *val)
   2039        1.1       jtc {
   2040        1.1       jtc 	char *expr;
   2041        1.1       jtc 	off_t num, t;
   2042        1.1       jtc 
   2043       1.33     lukem 	num = STRTOOFFT(val, &expr, 0);
   2044       1.33     lukem 	if ((num == OFFT_MAX) || (num <= 0) || (expr == val))
   2045       1.93       dsl 		return 0;
   2046        1.1       jtc 
   2047        1.1       jtc 	switch(*expr) {
   2048        1.1       jtc 	case 'b':
   2049        1.1       jtc 		t = num;
   2050        1.1       jtc 		num *= 512;
   2051        1.1       jtc 		if (t > num)
   2052       1.93       dsl 			return 0;
   2053        1.1       jtc 		++expr;
   2054        1.1       jtc 		break;
   2055        1.1       jtc 	case 'k':
   2056        1.1       jtc 		t = num;
   2057        1.1       jtc 		num *= 1024;
   2058        1.1       jtc 		if (t > num)
   2059       1.93       dsl 			return 0;
   2060        1.1       jtc 		++expr;
   2061        1.1       jtc 		break;
   2062        1.1       jtc 	case 'm':
   2063        1.1       jtc 		t = num;
   2064        1.1       jtc 		num *= 1048576;
   2065        1.1       jtc 		if (t > num)
   2066       1.93       dsl 			return 0;
   2067        1.1       jtc 		++expr;
   2068        1.1       jtc 		break;
   2069        1.1       jtc 	case 'w':
   2070        1.1       jtc 		t = num;
   2071        1.1       jtc 		num *= sizeof(int);
   2072        1.1       jtc 		if (t > num)
   2073       1.93       dsl 			return 0;
   2074        1.1       jtc 		++expr;
   2075        1.1       jtc 		break;
   2076        1.1       jtc 	}
   2077        1.1       jtc 
   2078        1.1       jtc 	switch(*expr) {
   2079        1.1       jtc 		case '\0':
   2080        1.1       jtc 			break;
   2081        1.1       jtc 		case '*':
   2082        1.1       jtc 		case 'x':
   2083        1.1       jtc 			t = num;
   2084        1.1       jtc 			num *= str_offt(expr + 1);
   2085        1.1       jtc 			if (t > num)
   2086       1.93       dsl 				return 0;
   2087        1.1       jtc 			break;
   2088        1.1       jtc 		default:
   2089       1.93       dsl 			return 0;
   2090        1.1       jtc 	}
   2091       1.93       dsl 	return num;
   2092        1.1       jtc }
   2093        1.1       jtc 
   2094      1.111     joerg static char *
   2095      1.105       roy get_line(FILE *f)
   2096       1.40  christos {
   2097       1.40  christos 	char *name, *temp;
   2098       1.40  christos 	size_t len;
   2099       1.40  christos 
   2100       1.40  christos 	name = fgetln(f, &len);
   2101       1.40  christos 	if (!name) {
   2102      1.105       roy 		get_line_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
   2103       1.93       dsl 		return 0;
   2104       1.40  christos 	}
   2105       1.40  christos 	if (name[len-1] != '\n')
   2106       1.40  christos 		len++;
   2107       1.40  christos 	temp = malloc(len);
   2108       1.40  christos 	if (!temp) {
   2109      1.105       roy 		get_line_error = GETLINE_OUT_OF_MEM;
   2110       1.93       dsl 		return 0;
   2111       1.40  christos 	}
   2112       1.40  christos 	memcpy(temp, name, len-1);
   2113       1.40  christos 	temp[len-1] = 0;
   2114       1.93       dsl 	return temp;
   2115       1.40  christos }
   2116       1.40  christos 
   2117      1.117  christos #ifndef SMALL
   2118      1.117  christos /*
   2119      1.117  christos  * set_tstamp()
   2120      1.117  christos  *	Use a specific timestamp for all individual files created in the
   2121      1.117  christos  *	archive
   2122      1.117  christos  */
   2123      1.117  christos static int
   2124      1.117  christos set_tstamp(const char *b, struct stat *st)
   2125      1.117  christos {
   2126      1.117  christos 	time_t when;
   2127      1.117  christos 	char *eb;
   2128      1.117  christos 	long long l;
   2129      1.117  christos 
   2130      1.117  christos 	if (stat(b, st) != -1)
   2131      1.117  christos 		return 0;
   2132      1.117  christos 
   2133      1.117  christos #ifndef HAVE_NBTOOL_CONFIG_H
   2134      1.117  christos 	errno = 0;
   2135      1.117  christos 	if ((when = parsedate(b, NULL, NULL)) == -1 && errno != 0)
   2136      1.117  christos #endif
   2137      1.117  christos 	{
   2138      1.117  christos 		errno = 0;
   2139      1.117  christos 		l = strtoll(b, &eb, 0);
   2140      1.117  christos 		if (b == eb || *eb || errno)
   2141      1.117  christos 			return -1;
   2142      1.117  christos 		when = (time_t)l;
   2143      1.117  christos 	}
   2144      1.117  christos 
   2145      1.117  christos 	st->st_ino = 1;
   2146      1.117  christos #if HAVE_STRUCT_STAT_BIRTHTIME
   2147      1.117  christos 	st->st_birthtime =
   2148      1.117  christos #endif
   2149      1.117  christos 	st->st_mtime = st->st_ctime = st->st_atime = when;
   2150      1.117  christos 	return 0;
   2151      1.117  christos }
   2152      1.117  christos #endif
   2153      1.117  christos 
   2154        1.1       jtc /*
   2155        1.1       jtc  * no_op()
   2156        1.1       jtc  *	for those option functions where the archive format has nothing to do.
   2157        1.1       jtc  * Return:
   2158        1.1       jtc  *	0
   2159        1.1       jtc  */
   2160        1.1       jtc 
   2161        1.1       jtc static int
   2162        1.1       jtc no_op(void)
   2163        1.1       jtc {
   2164       1.93       dsl 	return 0;
   2165        1.1       jtc }
   2166        1.1       jtc 
   2167        1.1       jtc /*
   2168        1.1       jtc  * pax_usage()
   2169        1.1       jtc  *	print the usage summary to the user
   2170        1.1       jtc  */
   2171        1.1       jtc 
   2172      1.111     joerg static void
   2173        1.1       jtc pax_usage(void)
   2174        1.1       jtc {
   2175       1.35     lukem 	fprintf(stderr,
   2176      1.101      hira "usage: pax [-0cdjnvzVO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
   2177       1.35     lukem "           [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
   2178       1.35     lukem "           [pattern ...]\n");
   2179       1.35     lukem 	fprintf(stderr,
   2180      1.101      hira "       pax -r [-cdijknuvzADOVYZ] [-E limit] [-f archive] [-N dbdir]\n"
   2181       1.35     lukem "           [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
   2182       1.35     lukem "           [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
   2183       1.35     lukem 	fprintf(stderr,
   2184      1.101      hira "       pax -w [-dijtuvzAHLMOPVX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
   2185       1.35     lukem "           [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
   2186       1.35     lukem "           [-U user] ... [-G group] ...\n"
   2187       1.35     lukem "           [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
   2188       1.35     lukem 	fprintf(stderr,
   2189      1.101      hira "       pax -r -w [-dijklntuvzADHLMOPVXYZ] [-N dbdir] [-p string] ...\n"
   2190       1.35     lukem "           [-s replstr] ... [-U user] ... [-G group] ...\n"
   2191       1.35     lukem "           [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
   2192        1.1       jtc 	exit(1);
   2193       1.12   mycroft 	/* NOTREACHED */
   2194        1.1       jtc }
   2195        1.1       jtc 
   2196        1.1       jtc /*
   2197        1.1       jtc  * tar_usage()
   2198        1.1       jtc  *	print the usage summary to the user
   2199        1.1       jtc  */
   2200        1.1       jtc 
   2201      1.111     joerg static void
   2202        1.1       jtc tar_usage(void)
   2203        1.1       jtc {
   2204      1.116  christos 	(void)fputs("usage: tar [-]{crtux}[-befhjklmopqvwzHJOPSXZ014578] "
   2205      1.116  christos 		    "[archive] [blocksize]\n"
   2206       1.44     soren 		    "           [-C directory] [-T file] [-s replstr] "
   2207       1.44     soren 		    "[file ...]\n", stderr);
   2208        1.1       jtc 	exit(1);
   2209       1.12   mycroft 	/* NOTREACHED */
   2210        1.1       jtc }
   2211        1.1       jtc 
   2212       1.89  christos #ifndef NO_CPIO
   2213        1.1       jtc /*
   2214        1.1       jtc  * cpio_usage()
   2215        1.1       jtc  *	print the usage summary to the user
   2216        1.1       jtc  */
   2217        1.1       jtc 
   2218      1.111     joerg static void
   2219        1.1       jtc cpio_usage(void)
   2220        1.1       jtc {
   2221       1.10       mrg 
   2222       1.71      jmmv 	(void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
   2223       1.51     soren 		    "[-H format] [-O archive]\n"
   2224       1.51     soren 		    "               < name-list [> archive]\n"
   2225       1.51     soren 		    "       cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
   2226       1.51     soren 		    "[-F archive] [-H format] \n"
   2227       1.51     soren 		    "               [-I archive] "
   2228       1.51     soren 		    "[pattern ...] [< archive]\n"
   2229       1.51     soren 		    "       cpio -p [-adlLmuv] destination-directory "
   2230       1.51     soren 		    "< name-list\n", stderr);
   2231        1.1       jtc 	exit(1);
   2232       1.12   mycroft 	/* NOTREACHED */
   2233        1.1       jtc }
   2234       1.89  christos #endif
   2235