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