Home | History | Annotate | Line # | Download | only in pax
options.c revision 1.98.2.1
      1 /*	$NetBSD: options.c,v 1.98.2.1 2009/08/25 18:07:09 snj 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.98.2.1 2009/08/25 18:07:09 snj 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 *get_line(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 get_line */
     99 #define GETLINE_FILE_CORRUPT 1
    100 #define GETLINE_OUT_OF_MEM 2
    101 static int get_line_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_options(int argc, char **argv)
    820 {
    821 	int c;
    822 	int fstdin = 0;
    823 	int Oflag = 0;
    824 	int nincfiles = 0;
    825 	int incfiles_max = 0;
    826 	struct incfile {
    827 		char *file;
    828 		char *dir;
    829 	};
    830 	struct incfile *incfiles = NULL;
    831 
    832 	/*
    833 	 * Set default values.
    834 	 */
    835 	rmleadslash = 1;
    836 	is_gnutar = 1;
    837 
    838 	/*
    839 	 * process option flags
    840 	 */
    841 	while ((c = getoldopt(argc, argv,
    842 	    "+b:cef:hjklmopqrs:tuvwxzBC:HI:OPST:X:Z014578",
    843 	    tar_longopts, NULL))
    844 	    != -1)  {
    845 		switch(c) {
    846 		case 'b':
    847 			/*
    848 			 * specify blocksize in 512-byte blocks
    849 			 */
    850 			if ((wrblksz = (int)str_offt(optarg)) <= 0) {
    851 				tty_warn(1, "Invalid block size %s", optarg);
    852 				tar_usage();
    853 			}
    854 			wrblksz *= 512;		/* XXX - check for int oflow */
    855 			break;
    856 		case 'c':
    857 			/*
    858 			 * create an archive
    859 			 */
    860 			act = ARCHIVE;
    861 			break;
    862 		case 'e':
    863 			/*
    864 			 * stop after first error
    865 			 */
    866 			maxflt = 0;
    867 			break;
    868 		case 'f':
    869 			/*
    870 			 * filename where the archive is stored
    871 			 */
    872 			if ((optarg[0] == '-') && (optarg[1]== '\0')) {
    873 				/*
    874 				 * treat a - as stdin
    875 				 */
    876 				fstdin = 1;
    877 				arcname = NULL;
    878 				break;
    879 			}
    880 			fstdin = 0;
    881 			arcname = optarg;
    882 			break;
    883 		case 'h':
    884 			/*
    885 			 * follow symlinks
    886 			 */
    887 			Lflag = 1;
    888 			break;
    889 		case 'j':
    890 			/*
    891 			 * pass through bzip2. not a standard option
    892 			 */
    893 			jflag = 1;
    894 			gzip_program = BZIP2_CMD;
    895 			break;
    896 		case 'k':
    897 			/*
    898 			 * do not clobber files that exist
    899 			 */
    900 			kflag = 1;
    901 			break;
    902 		case 'l':
    903 			/*
    904 			 * do not pass over mount points in the file system
    905 			 */
    906 			Xflag = 1;
    907 			break;
    908 		case 'm':
    909 			/*
    910 			 * do not preserve modification time
    911 			 */
    912 			pmtime = 0;
    913 			break;
    914 		case 'o':
    915 			/*
    916 			 * This option does several things based on whether
    917 			 * this is a create or extract operation.
    918 			 */
    919 			if (act == ARCHIVE) {
    920 				/* GNU tar: write V7 format archives. */
    921 				Oflag = 1;
    922 				/* 4.2BSD: don't add directory entries. */
    923 				if (opt_add("write_opt=nodir") < 0)
    924 					tar_usage();
    925 
    926 			} else {
    927 				/* SUS: don't preserve owner/group. */
    928 				pids = 0;
    929 				nopids = 1;
    930 			}
    931 			break;
    932 		case 'O':
    933 			Oflag = 1;
    934 			break;
    935 		case 'p':
    936 			/*
    937 			 * preserve user id, group id, file
    938 			 * mode, access/modification times
    939 			 */
    940 			if (!nopids)
    941 				pids = 1;
    942 			pmode = 1;
    943 			patime = 1;
    944 			pmtime = 1;
    945 			break;
    946 		case 'q':
    947 			/*
    948 			 * select first match for a pattern only
    949 			 */
    950 			nflag = 1;
    951 			break;
    952 		case 'r':
    953 		case 'u':
    954 			/*
    955 			 * append to the archive
    956 			 */
    957 			act = APPND;
    958 			break;
    959 		case 's':
    960 			/*
    961 			 * file name substitution name pattern
    962 			 */
    963 			if (rep_add(optarg) < 0) {
    964 				tar_usage();
    965 				break;
    966 			}
    967 			break;
    968 		case 't':
    969 			/*
    970 			 * list contents of the tape
    971 			 */
    972 			act = LIST;
    973 			break;
    974 		case 'v':
    975 			/*
    976 			 * verbose operation mode
    977 			 */
    978 			vflag = 1;
    979 			break;
    980 		case 'w':
    981 			/*
    982 			 * interactive file rename
    983 			 */
    984 			iflag = 1;
    985 			break;
    986 		case 'x':
    987 			/*
    988 			 * extract an archive, preserving mode,
    989 			 * and mtime if possible.
    990 			 */
    991 			act = EXTRACT;
    992 			pmtime = 1;
    993 			break;
    994 		case 'z':
    995 			/*
    996 			 * use gzip.  Non standard option.
    997 			 */
    998 			zflag = 1;
    999 			gzip_program = GZIP_CMD;
   1000 			break;
   1001 		case 'B':
   1002 			/*
   1003 			 * Nothing to do here, this is pax default
   1004 			 */
   1005 			break;
   1006 		case 'C':
   1007 			havechd++;
   1008 			chdname = optarg;
   1009 			break;
   1010 		case 'H':
   1011 			/*
   1012 			 * follow command line symlinks only
   1013 			 */
   1014 			Hflag = 1;
   1015 			break;
   1016 		case 'I':
   1017 		case 'T':
   1018 			if (++nincfiles > incfiles_max) {
   1019 				incfiles_max = nincfiles + 3;
   1020 				incfiles = realloc(incfiles,
   1021 				    sizeof(*incfiles) * incfiles_max);
   1022 				if (incfiles == NULL) {
   1023 					tty_warn(0, "Unable to allocate space "
   1024 					    "for option list");
   1025 					exit(1);
   1026 				}
   1027 			}
   1028 			incfiles[nincfiles - 1].file = optarg;
   1029 			incfiles[nincfiles - 1].dir = chdname;
   1030 			break;
   1031 		case 'P':
   1032 			/*
   1033 			 * do not remove leading '/' from pathnames
   1034 			 */
   1035 			rmleadslash = 0;
   1036 			Aflag = 1;
   1037 			break;
   1038 		case 'S':
   1039 			/* do nothing; we already generate sparse files */
   1040 			break;
   1041 		case 'X':
   1042 			/*
   1043 			 * GNU tar compat: exclude the files listed in optarg
   1044 			 */
   1045 			if (tar_gnutar_X_compat(optarg) != 0)
   1046 				tar_usage();
   1047 			break;
   1048 		case 'Z':
   1049 			/*
   1050 			 * use compress.
   1051 			 */
   1052 			zflag = 1;
   1053 			gzip_program = COMPRESS_CMD;
   1054 			break;
   1055 		case '0':
   1056 			arcname = DEV_0;
   1057 			break;
   1058 		case '1':
   1059 			arcname = DEV_1;
   1060 			break;
   1061 		case '4':
   1062 			arcname = DEV_4;
   1063 			break;
   1064 		case '5':
   1065 			arcname = DEV_5;
   1066 			break;
   1067 		case '7':
   1068 			arcname = DEV_7;
   1069 			break;
   1070 		case '8':
   1071 			arcname = DEV_8;
   1072 			break;
   1073 		case OPT_ATIME_PRESERVE:
   1074 			patime = 1;
   1075 			break;
   1076 		case OPT_UNLINK:
   1077 			/* Just ignore -- we always unlink first. */
   1078 			break;
   1079 		case OPT_USE_COMPRESS_PROGRAM:
   1080 			zflag = 1;
   1081 			gzip_program = optarg;
   1082 			break;
   1083 		case OPT_FORCE_LOCAL:
   1084 			forcelocal = 1;
   1085 			break;
   1086 		case OPT_INSECURE:
   1087 			secure = 0;
   1088 			break;
   1089 		case OPT_STRICT:
   1090 			/* disable gnu extensions */
   1091 			is_gnutar = 0;
   1092 			break;
   1093 		case OPT_EXCLUDE:
   1094 			if (tar_gnutar_minus_minus_exclude(optarg) != 0)
   1095 				tar_usage();
   1096 			break;
   1097 		case OPT_NORECURSE:
   1098 			dflag = 1;
   1099 			break;
   1100 #if !HAVE_NBTOOL_CONFIG_H
   1101 		case OPT_CHROOT:
   1102 			do_chroot = 1;
   1103 			break;
   1104 #endif
   1105 		default:
   1106 			tar_usage();
   1107 			break;
   1108 		}
   1109 	}
   1110 	argc -= optind;
   1111 	argv += optind;
   1112 
   1113 	/* Tar requires an action. */
   1114 	if (act == ERROR)
   1115 		tar_usage();
   1116 
   1117 	/* Traditional tar behaviour (pax uses stderr unless in list mode) */
   1118 	if (fstdin == 1 && act == ARCHIVE)
   1119 		listf = stderr;
   1120 	else
   1121 		listf = stdout;
   1122 
   1123 	/* Traditional tar behaviour (pax wants to read file list from stdin) */
   1124 	if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
   1125 		exit(0);
   1126 	/*
   1127 	 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
   1128 	 * (unless -o specified)
   1129 	 */
   1130 	if (act == ARCHIVE || act == APPND)
   1131 		frmt = &(fsub[Oflag ? F_TAR : F_USTAR]);
   1132 	else if (Oflag) {
   1133 		if (act == EXTRACT)
   1134 			to_stdout = 1;
   1135 		else {
   1136 			tty_warn(1, "The -O/-o options are only valid when "
   1137 			    "writing or extracting an archive");
   1138 			tar_usage();
   1139 		}
   1140 	}
   1141 
   1142 	/*
   1143 	 * process the args as they are interpreted by the operation mode
   1144 	 */
   1145 	switch (act) {
   1146 	case LIST:
   1147 	case EXTRACT:
   1148 	default:
   1149 		{
   1150 			int sawpat = 0;
   1151 			int dirisnext = 0;
   1152 			char *file, *dir = NULL;
   1153 
   1154 			while (nincfiles || *argv != NULL) {
   1155 				/*
   1156 				 * If we queued up any include files,
   1157 				 * pull them in now.  Otherwise, check
   1158 				 * for -I and -C positional flags.
   1159 				 * Anything else must be a file to
   1160 				 * extract.
   1161 				 */
   1162 				if (nincfiles) {
   1163 					file = incfiles->file;
   1164 					dir = incfiles->dir;
   1165 					incfiles++;
   1166 					nincfiles--;
   1167 				} else if (strcmp(*argv, "-I") == 0) {
   1168 					if (*++argv == NULL)
   1169 						break;
   1170 					file = *argv++;
   1171 					dir = chdname;
   1172 				} else {
   1173 					file = NULL;
   1174 					dir = NULL;
   1175 				}
   1176 				if (file != NULL) {
   1177 					FILE *fp;
   1178 					char *str;
   1179 
   1180 					if (strcmp(file, "-") == 0)
   1181 						fp = stdin;
   1182 					else if ((fp = fopen(file, "r")) == NULL) {
   1183 						tty_warn(1, "Unable to open file '%s' for read", file);
   1184 						tar_usage();
   1185 					}
   1186 					while ((str = get_line(fp)) != NULL) {
   1187 						if (dirisnext) {
   1188 							if (dir)
   1189 								free(dir);
   1190 							dir = str;
   1191 							dirisnext = 0;
   1192 							continue;
   1193 						}
   1194 						if (strcmp(str, "-C") == 0) {
   1195 							havechd++;
   1196 							dirisnext = 1;
   1197 							free(str);
   1198 							continue;
   1199 						}
   1200 						if (strncmp(str, "-C ", 3) == 0) {
   1201 							havechd++;
   1202 							if (dir)
   1203 								free(dir);
   1204 							dir = strdup(str + 3);
   1205 							free(str);
   1206 							continue;
   1207 						}
   1208 						if (pat_add(str, dir) < 0)
   1209 							tar_usage();
   1210 						sawpat = 1;
   1211 					}
   1212 					/* Bomb if given -C w/out a dir. */
   1213 					if (dirisnext)
   1214 						tar_usage();
   1215 					if (dir)
   1216 						free(dir);
   1217 					if (strcmp(file, "-") != 0)
   1218 						fclose(fp);
   1219 					if (get_line_error) {
   1220 						tty_warn(1, "Problem with file '%s'", file);
   1221 						tar_usage();
   1222 					}
   1223 				} else if (strcmp(*argv, "-C") == 0) {
   1224 					if (*++argv == NULL)
   1225  						break;
   1226 					chdname = *argv++;
   1227 					havechd++;
   1228 				} else if (pat_add(*argv++, chdname) < 0)
   1229 					tar_usage();
   1230 				else
   1231 					sawpat = 1;
   1232 			}
   1233 			/*
   1234 			 * if patterns were added, we are doing	chdir()
   1235 			 * on a file-by-file basis, else, just one
   1236 			 * global chdir (if any) after opening input.
   1237 			 */
   1238 			if (sawpat > 0)
   1239 				chdname = NULL;
   1240 		}
   1241 		break;
   1242 	case ARCHIVE:
   1243 	case APPND:
   1244 		if (chdname != NULL) {	/* initial chdir() */
   1245 			if (ftree_add(chdname, 1) < 0)
   1246 				tar_usage();
   1247 		}
   1248 
   1249 		while (nincfiles || *argv != NULL) {
   1250 			char *file, *dir;
   1251 
   1252 			/*
   1253 			 * If we queued up any include files, pull them in
   1254 			 * now.  Otherwise, check for -I and -C positional
   1255 			 * flags.  Anything else must be a file to include
   1256 			 * in the archive.
   1257 			 */
   1258 			if (nincfiles) {
   1259 				file = incfiles->file;
   1260 				dir = incfiles->dir;
   1261 				incfiles++;
   1262 				nincfiles--;
   1263 			} else if (strcmp(*argv, "-I") == 0) {
   1264 				if (*++argv == NULL)
   1265 					break;
   1266 				file = *argv++;
   1267 				dir = NULL;
   1268 			} else {
   1269 				file = NULL;
   1270 				dir = NULL;
   1271 			}
   1272 			if (file != NULL) {
   1273 				FILE *fp;
   1274 				char *str;
   1275 				int dirisnext = 0;
   1276 
   1277 				/* Set directory if needed */
   1278 				if (dir) {
   1279 					if (ftree_add(dir, 1) < 0)
   1280 						tar_usage();
   1281 				}
   1282 
   1283 				if (strcmp(file, "-") == 0)
   1284 					fp = stdin;
   1285 				else if ((fp = fopen(file, "r")) == NULL) {
   1286 					tty_warn(1, "Unable to open file '%s' for read", file);
   1287 					tar_usage();
   1288 				}
   1289 				while ((str = get_line(fp)) != NULL) {
   1290 					if (dirisnext) {
   1291 						if (ftree_add(str, 1) < 0)
   1292 							tar_usage();
   1293 						dirisnext = 0;
   1294 						continue;
   1295 					}
   1296 					if (strcmp(str, "-C") == 0) {
   1297 						dirisnext = 1;
   1298 						continue;
   1299 					}
   1300 					if (strncmp(str, "-C ", 3) == 0) {
   1301 						if (ftree_add(str + 3, 1) < 0)
   1302 							tar_usage();
   1303 						continue;
   1304 					}
   1305 					if (ftree_add(str, 0) < 0)
   1306 						tar_usage();
   1307 				}
   1308 				/* Bomb if given -C w/out a dir. */
   1309 				if (dirisnext)
   1310 					tar_usage();
   1311 				if (strcmp(file, "-") != 0)
   1312 					fclose(fp);
   1313 				if (get_line_error) {
   1314 					tty_warn(1, "Problem with file '%s'",
   1315 					    file);
   1316 					tar_usage();
   1317 				}
   1318 			} else if (strcmp(*argv, "-C") == 0) {
   1319 				if (*++argv == NULL)
   1320 					break;
   1321 				if (ftree_add(*argv++, 1) < 0)
   1322 					tar_usage();
   1323 			} else if (ftree_add(*argv++, 0) < 0)
   1324 				tar_usage();
   1325 		}
   1326 		/*
   1327 		 * no read errors allowed on updates/append operation!
   1328 		 */
   1329 		maxflt = 0;
   1330 		break;
   1331 	}
   1332 	if (!fstdin && ((arcname == (char *)NULL) || (*arcname == '\0'))) {
   1333 		arcname = getenv("TAPE");
   1334 		if ((arcname == NULL) || (*arcname == '\0'))
   1335 			arcname = _PATH_DEFTAPE;
   1336 	}
   1337 }
   1338 
   1339 int
   1340 mkpath(path)
   1341 	char *path;
   1342 {
   1343 	char *slash;
   1344 	int done = 0;
   1345 
   1346 	slash = path;
   1347 
   1348 	while (!done) {
   1349 		slash += strspn(slash, "/");
   1350 		slash += strcspn(slash, "/");
   1351 
   1352 		done = (*slash == '\0');
   1353 		*slash = '\0';
   1354 
   1355 		if (domkdir(path, 0777) == -1)
   1356 			goto out;
   1357 
   1358 		if (!done)
   1359 			*slash = '/';
   1360 	}
   1361 
   1362 	return 0;
   1363 out:
   1364 	/* Can't create or or not a directory */
   1365 	syswarn(1, errno, "Cannot create directory `%s'", path);
   1366 	return -1;
   1367 }
   1368 
   1369 
   1370 #ifndef NO_CPIO
   1371 struct option cpio_longopts[] = {
   1372 	{ "reset-access-time",	no_argument,		0,	'a' },
   1373 	{ "make-directories",	no_argument,		0, 	'd' },
   1374 	{ "nonmatching",	no_argument,		0,	'f' },
   1375 	{ "extract",		no_argument,		0,	'i' },
   1376 	{ "link",		no_argument,		0,	'l' },
   1377 	{ "preserve-modification-time", no_argument,	0,	'm' },
   1378 	{ "create",		no_argument,		0,	'o' },
   1379 	{ "pass-through",	no_argument,		0,	'p' },
   1380 	{ "rename",		no_argument,		0,	'r' },
   1381 	{ "list",		no_argument,		0,	't' },
   1382 	{ "unconditional",	no_argument,		0,	'u' },
   1383 	{ "verbose",		no_argument,		0,	'v' },
   1384 	{ "append",		no_argument,		0,	'A' },
   1385 	{ "pattern-file",	required_argument,	0,	'E' },
   1386 	{ "file",		required_argument,	0,	'F' },
   1387 	{ "force-local",	no_argument,		0,
   1388 						OPT_FORCE_LOCAL },
   1389 	{ "format",		required_argument,	0,	'H' },
   1390 	{ "dereference",	no_argument,		0,	'L' },
   1391 	{ "swap-halfwords",	no_argument,		0,	'S' },
   1392 	{ "insecure",		no_argument,		0,
   1393 						OPT_INSECURE },
   1394 	{ "sparse",		no_argument,		0,
   1395 						OPT_SPARSE },
   1396 
   1397 #ifdef notyet
   1398 /* Not implemented */
   1399 	{ "null",		no_argument,		0,	'0' },
   1400 	{ "swap",		no_argument,		0,	'b' },
   1401 	{ "numeric-uid-gid",	no_argument,		0,	'n' },
   1402 	{ "swap-bytes",		no_argument,		0,	's' },
   1403 	{ "message",		required_argument,	0,	'M' },
   1404 	{ "owner",		required_argument,	0	'R' },
   1405 	{ "dot",		no_argument,		0,	'V' },
   1406 	{ "block-size",		required_argument,	0,
   1407 						OPT_BLOCK_SIZE },
   1408 	{ "no-absolute-pathnames", no_argument,		0,
   1409 						OPT_NO_ABSOLUTE_PATHNAMES },
   1410 	{ "no-preserve-owner",	no_argument,		0,
   1411 						OPT_NO_PRESERVE_OWNER },
   1412 	{ "only-verify-crc",	no_argument,		0,
   1413 						OPT_ONLY_VERIFY_CRC },
   1414 	{ "rsh-command",	required_argument,	0,
   1415 						OPT_RSH_COMMAND },
   1416 	{ "version",		no_argument,		0,
   1417 						OPT_VERSION },
   1418 #endif
   1419 	{ 0,			0,			0,	0 },
   1420 };
   1421 
   1422 /*
   1423  * cpio_options()
   1424  *	look at the user specified flags. set globals as required and check if
   1425  *	the user specified a legal set of flags. If not, complain and exit
   1426  */
   1427 
   1428 static void
   1429 cpio_options(int argc, char **argv)
   1430 {
   1431 	FSUB tmp;
   1432 	unsigned int flg = 0;
   1433 	unsigned int bflg = 0;
   1434 	int c, i;
   1435 	FILE *fp;
   1436 	char *str;
   1437 
   1438 	uflag = 1;
   1439 	kflag = 1;
   1440 	pids = 1;
   1441 	pmode = 1;
   1442 	pmtime = 0;
   1443 	arcname = NULL;
   1444 	dflag = 1;
   1445 	nodirs = 1;
   1446 	/*
   1447 	 * process option flags
   1448 	 */
   1449 	while ((c = getoldopt(argc, argv,
   1450 	    "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
   1451 	    cpio_longopts, NULL)) != -1) {
   1452 		switch(c) {
   1453 		case 'a':
   1454 			/*
   1455 			 * preserve access time on filesystem nodes we read
   1456 			 */
   1457 			tflag = 1;
   1458 			flg |= TF;
   1459 			break;
   1460 #ifdef notyet
   1461 		case 'b':
   1462 			/*
   1463 			 * swap bytes and half-words when reading data
   1464 			 */
   1465 			break;
   1466 #endif
   1467 		case 'c':
   1468 			/*
   1469 			 * ASCII cpio header
   1470 			 */
   1471 			frmt = &fsub[F_SV4CPIO];
   1472 			break;
   1473 		case 'd':
   1474 			/*
   1475 			 * create directories as needed
   1476 			 * pax does this by default ..
   1477 			 */
   1478 			nodirs = 0;
   1479 			break;
   1480 		case 'f':
   1481 			/*
   1482 			 * inverse match on patterns
   1483 			 */
   1484 			cflag = 1;
   1485 			flg |= CF;
   1486 			break;
   1487 		case 'i':
   1488 			/*
   1489 			 * read the archive
   1490 			 */
   1491 			act = EXTRACT;
   1492 			flg |= RF;
   1493 			break;
   1494 #ifdef notyet
   1495 		case 'k':
   1496 			break;
   1497 #endif
   1498 		case 'l':
   1499 			/*
   1500 			 * try to link src to dest with copy (-rw)
   1501 			 */
   1502 			lflag = 1;
   1503 			flg |= LF;
   1504 			break;
   1505 		case 'm':
   1506 			/*
   1507 			 * preserve mtime
   1508 			 */
   1509 			flg |= PF;
   1510 			pmtime = 1;
   1511 			break;
   1512 		case 'o':
   1513 			/*
   1514 			 * write an archive
   1515 			 */
   1516 			act = ARCHIVE;
   1517 			frmt = &(fsub[F_SV4CRC]);
   1518 			flg |= WF;
   1519 			break;
   1520 		case 'p':
   1521 			/*
   1522 			 * cpio -p is like pax -rw
   1523 			 */
   1524 			act = COPY;
   1525 			flg |= RF | WF;
   1526 			break;
   1527 		case 'r':
   1528 			/*
   1529 			 * interactive file rename
   1530 			 */
   1531 			iflag = 1;
   1532 			flg |= IF;
   1533 			break;
   1534 #ifdef notyet
   1535 		case 's':
   1536 			/*
   1537 			 * swap bytes after reading data
   1538 			 */
   1539 			break;
   1540 #endif
   1541 		case 't':
   1542 			/*
   1543 			 * list contents of archive
   1544 			 */
   1545 			act = LIST;
   1546 			listf = stdout;
   1547 			flg &= ~RF;
   1548 			break;
   1549 		case 'u':
   1550 			/*
   1551 			 * don't ignore those older files
   1552 			 */
   1553 			uflag = 0;
   1554 			kflag = 0;
   1555 			flg |= UF;
   1556 			break;
   1557 		case 'v':
   1558 			/*
   1559 			 * verbose operation mode
   1560 			 */
   1561 			vflag = 1;
   1562 			flg |= VF;
   1563 			break;
   1564 		case 'z':
   1565 			/*
   1566 			 * use gzip.  Non standard option.
   1567 			 */
   1568 			gzip_program = GZIP_CMD;
   1569 			break;
   1570 		case 'A':
   1571 			/*
   1572 			 * append to an archive
   1573 			 */
   1574 			act = APPND;
   1575 			flg |= AF;
   1576 			break;
   1577 		case 'B':
   1578 			/*
   1579 			 * set blocksize to 5120
   1580 			 */
   1581 			blksz = 5120;
   1582 			break;
   1583 		case 'C':
   1584 			/*
   1585 			 * specify blocksize
   1586 			 */
   1587 			if ((blksz = (int)str_offt(optarg)) <= 0) {
   1588 				tty_warn(1, "Invalid block size %s", optarg);
   1589 				cpio_usage();
   1590 			}
   1591 			break;
   1592 		case 'E':
   1593 			/*
   1594 			 * file with patterns to extract or list
   1595 			 */
   1596 			if ((fp = fopen(optarg, "r")) == NULL) {
   1597 				tty_warn(1, "Unable to open file '%s' for read",
   1598 				    optarg);
   1599 				cpio_usage();
   1600 			}
   1601 			while ((str = get_line(fp)) != NULL) {
   1602 				pat_add(str, NULL);
   1603 			}
   1604 			fclose(fp);
   1605 			if (get_line_error) {
   1606 				tty_warn(1, "Problem with file '%s'", optarg);
   1607 				cpio_usage();
   1608 			}
   1609 			break;
   1610 		case 'H':
   1611 			/*
   1612 			 * specify an archive format on write
   1613 			 */
   1614 			tmp.name = optarg;
   1615 			frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
   1616 			    sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
   1617 			if (frmt != NULL) {
   1618 				flg |= XF;
   1619 				break;
   1620 			}
   1621 			tty_warn(1, "Unknown -H format: %s", optarg);
   1622 			(void)fputs("cpio: Known -H formats are:", stderr);
   1623 			for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
   1624 				(void)fprintf(stderr, " %s", fsub[i].name);
   1625 			(void)fputs("\n\n", stderr);
   1626 			cpio_usage();
   1627 			break;
   1628 		case 'I':
   1629 		case 'O':
   1630 			/*
   1631 			 * filename where the archive is stored
   1632 			 */
   1633 			if ((optarg[0] == '-') && (optarg[1]== '\0')) {
   1634 				/*
   1635 				 * treat a - as stdin
   1636 				 */
   1637 				arcname = NULL;
   1638 				break;
   1639 			}
   1640 			arcname = optarg;
   1641 			break;
   1642 		case 'L':
   1643 			/*
   1644 			 * follow symlinks
   1645 			 */
   1646 			Lflag = 1;
   1647 			flg |= CLF;
   1648 			break;
   1649 #ifdef notyet
   1650 		case 'M':
   1651 			arg = optarg;
   1652 			break;
   1653 		case 'R':
   1654 			arg = optarg;
   1655 			break;
   1656 #endif
   1657 		case 'S':
   1658 			/*
   1659 			 * swap halfwords after reading data
   1660 			 */
   1661 			cpio_swp_head = 1;
   1662 			break;
   1663 #ifdef notyet
   1664 		case 'V':
   1665 			break;
   1666 #endif
   1667 		case 'Z':
   1668 			/*
   1669 			 * use compress.  Non standard option.
   1670 			 */
   1671 			gzip_program = COMPRESS_CMD;
   1672 			break;
   1673 		case '6':
   1674 			/*
   1675 			 * process Version 6 cpio format
   1676 			 */
   1677 			frmt = &(fsub[F_BCPIO]);
   1678 		case OPT_FORCE_LOCAL:
   1679 			forcelocal = 1;
   1680 			break;
   1681 		case OPT_INSECURE:
   1682 			secure = 0;
   1683 			break;
   1684 
   1685 		case OPT_SPARSE:
   1686 			/* do nothing; we already generate sparse files */
   1687 			break;
   1688 		default:
   1689 			cpio_usage();
   1690 			break;
   1691 		}
   1692 	}
   1693 
   1694 	/*
   1695 	 * figure out the operation mode of cpio. check that we have not been
   1696 	 * given a bogus set of flags for the operation mode.
   1697 	 */
   1698 	if (ISLIST(flg)) {
   1699 		act = LIST;
   1700 		bflg = flg & BDLIST;
   1701 	} else if (ISEXTRACT(flg)) {
   1702 		act = EXTRACT;
   1703 		bflg = flg & BDEXTR;
   1704 	} else if (ISARCHIVE(flg)) {
   1705 		act = ARCHIVE;
   1706 		bflg = flg & BDARCH;
   1707 	} else if (ISAPPND(flg)) {
   1708 		act = APPND;
   1709 		bflg = flg & BDARCH;
   1710 	} else if (ISCOPY(flg)) {
   1711 		act = COPY;
   1712 		bflg = flg & BDCOPY;
   1713 	} else
   1714 		cpio_usage();
   1715 	if (bflg) {
   1716 		cpio_usage();
   1717 	}
   1718 
   1719 	/*
   1720 	 * if we are writing (ARCHIVE) we use the default format if the user
   1721 	 * did not specify a format. when we write during an APPEND, we will
   1722 	 * adopt the format of the existing archive if none was supplied.
   1723 	 */
   1724 	if (!(flg & XF) && (act == ARCHIVE))
   1725 		frmt = &(fsub[F_BCPIO]);
   1726 
   1727 	/*
   1728 	 * process the args as they are interpreted by the operation mode
   1729 	 */
   1730 	switch (act) {
   1731 	case LIST:
   1732 	case EXTRACT:
   1733 		for (; optind < argc; optind++)
   1734 			if (pat_add(argv[optind], 0) < 0)
   1735 				cpio_usage();
   1736 		break;
   1737 	case COPY:
   1738 		if (optind >= argc) {
   1739 			tty_warn(0, "Destination directory was not supplied");
   1740 			cpio_usage();
   1741 		}
   1742 		--argc;
   1743 		dirptr = argv[argc];
   1744 		/* FALLTHROUGH */
   1745 	case ARCHIVE:
   1746 	case APPND:
   1747 		if (argc != optind) {
   1748 			for (; optind < argc; optind++)
   1749 				if (ftree_add(argv[optind], 0) < 0)
   1750 					cpio_usage();
   1751 			break;
   1752 		}
   1753 		/*
   1754 		 * no read errors allowed on updates/append operation!
   1755 		 */
   1756 		maxflt = 0;
   1757 		while ((str = get_line(stdin)) != NULL) {
   1758 			ftree_add(str, 0);
   1759 		}
   1760 		if (get_line_error) {
   1761 			tty_warn(1, "Problem while reading stdin");
   1762 			cpio_usage();
   1763 		}
   1764 		break;
   1765 	default:
   1766 		cpio_usage();
   1767 		break;
   1768 	}
   1769 }
   1770 #endif
   1771 
   1772 /*
   1773  * printflg()
   1774  *	print out those invalid flag sets found to the user
   1775  */
   1776 
   1777 static void
   1778 printflg(unsigned int flg)
   1779 {
   1780 	int nxt;
   1781 
   1782 	(void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
   1783 	while ((nxt = ffs(flg)) != 0) {
   1784 		flg &= ~(1 << (nxt - 1));
   1785 		(void)fprintf(stderr, " -%c", flgch[nxt - 1]);
   1786 	}
   1787 	(void)putc('\n', stderr);
   1788 }
   1789 
   1790 /*
   1791  * c_frmt()
   1792  *	comparison routine used by bsearch to find the format specified
   1793  *	by the user
   1794  */
   1795 
   1796 static int
   1797 c_frmt(const void *a, const void *b)
   1798 {
   1799 	return strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name);
   1800 }
   1801 
   1802 /*
   1803  * opt_next()
   1804  *	called by format specific options routines to get each format specific
   1805  *	flag and value specified with -o
   1806  * Return:
   1807  *	pointer to next OPLIST entry or NULL (end of list).
   1808  */
   1809 
   1810 OPLIST *
   1811 opt_next(void)
   1812 {
   1813 	OPLIST *opt;
   1814 
   1815 	if ((opt = ophead) != NULL)
   1816 		ophead = ophead->fow;
   1817 	return opt;
   1818 }
   1819 
   1820 /*
   1821  * bad_opt()
   1822  *	generic routine used to complain about a format specific options
   1823  *	when the format does not support options.
   1824  */
   1825 
   1826 int
   1827 bad_opt(void)
   1828 {
   1829 	OPLIST *opt;
   1830 
   1831 	if (ophead == NULL)
   1832 		return 0;
   1833 	/*
   1834 	 * print all we were given
   1835 	 */
   1836 	tty_warn(1," These format options are not supported for %s",
   1837 	    frmt->name);
   1838 	while ((opt = opt_next()) != NULL)
   1839 		(void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
   1840 	if (strcmp(NM_TAR, argv0) == 0)
   1841 		tar_usage();
   1842 #ifndef NO_CPIO
   1843 	else if (strcmp(NM_CPIO, argv0) == 0)
   1844 		cpio_usage();
   1845 #endif
   1846 	else
   1847 		pax_usage();
   1848 	return 0;
   1849 }
   1850 
   1851 /*
   1852  * opt_add()
   1853  *	breaks the value supplied to -o into a option name and value. options
   1854  *	are given to -o in the form -o name-value,name=value
   1855  *	multiple -o may be specified.
   1856  * Return:
   1857  *	0 if format in name=value format, -1 if -o is passed junk
   1858  */
   1859 
   1860 int
   1861 opt_add(const char *str)
   1862 {
   1863 	OPLIST *opt;
   1864 	char *frpt;
   1865 	char *pt;
   1866 	char *endpt;
   1867 	char *dstr;
   1868 
   1869 	if ((str == NULL) || (*str == '\0')) {
   1870 		tty_warn(0, "Invalid option name");
   1871 		return -1;
   1872 	}
   1873 	if ((dstr = strdup(str)) == NULL) {
   1874 		tty_warn(0, "Unable to allocate space for option list");
   1875 		return -1;
   1876 	}
   1877 	frpt = endpt = dstr;
   1878 
   1879 	/*
   1880 	 * break into name and values pieces and stuff each one into a
   1881 	 * OPLIST structure. When we know the format, the format specific
   1882 	 * option function will go through this list
   1883 	 */
   1884 	while ((frpt != NULL) && (*frpt != '\0')) {
   1885 		if ((endpt = strchr(frpt, ',')) != NULL)
   1886 			*endpt = '\0';
   1887 		if ((pt = strchr(frpt, '=')) == NULL) {
   1888 			tty_warn(0, "Invalid options format");
   1889 			free(dstr);
   1890 			return -1;
   1891 		}
   1892 		if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
   1893 			tty_warn(0, "Unable to allocate space for option list");
   1894 			free(dstr);
   1895 			return -1;
   1896 		}
   1897 		*pt++ = '\0';
   1898 		opt->name = frpt;
   1899 		opt->value = pt;
   1900 		opt->fow = NULL;
   1901 		if (endpt != NULL)
   1902 			frpt = endpt + 1;
   1903 		else
   1904 			frpt = NULL;
   1905 		if (ophead == NULL) {
   1906 			optail = ophead = opt;
   1907 			continue;
   1908 		}
   1909 		optail->fow = opt;
   1910 		optail = opt;
   1911 	}
   1912 	return 0;
   1913 }
   1914 
   1915 /*
   1916  * str_offt()
   1917  *	Convert an expression of the following forms to an off_t > 0.
   1918  *	1) A positive decimal number.
   1919  *	2) A positive decimal number followed by a b (mult by 512).
   1920  *	3) A positive decimal number followed by a k (mult by 1024).
   1921  *	4) A positive decimal number followed by a m (mult by 512).
   1922  *	5) A positive decimal number followed by a w (mult by sizeof int)
   1923  *	6) Two or more positive decimal numbers (with/without k,b or w).
   1924  *	   separated by x (also * for backwards compatibility), specifying
   1925  *	   the product of the indicated values.
   1926  * Return:
   1927  *	0 for an error, a positive value o.w.
   1928  */
   1929 
   1930 static off_t
   1931 str_offt(char *val)
   1932 {
   1933 	char *expr;
   1934 	off_t num, t;
   1935 
   1936 	num = STRTOOFFT(val, &expr, 0);
   1937 	if ((num == OFFT_MAX) || (num <= 0) || (expr == val))
   1938 		return 0;
   1939 
   1940 	switch(*expr) {
   1941 	case 'b':
   1942 		t = num;
   1943 		num *= 512;
   1944 		if (t > num)
   1945 			return 0;
   1946 		++expr;
   1947 		break;
   1948 	case 'k':
   1949 		t = num;
   1950 		num *= 1024;
   1951 		if (t > num)
   1952 			return 0;
   1953 		++expr;
   1954 		break;
   1955 	case 'm':
   1956 		t = num;
   1957 		num *= 1048576;
   1958 		if (t > num)
   1959 			return 0;
   1960 		++expr;
   1961 		break;
   1962 	case 'w':
   1963 		t = num;
   1964 		num *= sizeof(int);
   1965 		if (t > num)
   1966 			return 0;
   1967 		++expr;
   1968 		break;
   1969 	}
   1970 
   1971 	switch(*expr) {
   1972 		case '\0':
   1973 			break;
   1974 		case '*':
   1975 		case 'x':
   1976 			t = num;
   1977 			num *= str_offt(expr + 1);
   1978 			if (t > num)
   1979 				return 0;
   1980 			break;
   1981 		default:
   1982 			return 0;
   1983 	}
   1984 	return num;
   1985 }
   1986 
   1987 char *
   1988 get_line(FILE *f)
   1989 {
   1990 	char *name, *temp;
   1991 	size_t len;
   1992 
   1993 	name = fgetln(f, &len);
   1994 	if (!name) {
   1995 		get_line_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
   1996 		return 0;
   1997 	}
   1998 	if (name[len-1] != '\n')
   1999 		len++;
   2000 	temp = malloc(len);
   2001 	if (!temp) {
   2002 		get_line_error = GETLINE_OUT_OF_MEM;
   2003 		return 0;
   2004 	}
   2005 	memcpy(temp, name, len-1);
   2006 	temp[len-1] = 0;
   2007 	return temp;
   2008 }
   2009 
   2010 /*
   2011  * no_op()
   2012  *	for those option functions where the archive format has nothing to do.
   2013  * Return:
   2014  *	0
   2015  */
   2016 
   2017 static int
   2018 no_op(void)
   2019 {
   2020 	return 0;
   2021 }
   2022 
   2023 /*
   2024  * pax_usage()
   2025  *	print the usage summary to the user
   2026  */
   2027 
   2028 void
   2029 pax_usage(void)
   2030 {
   2031 	fprintf(stderr,
   2032 "usage: pax [-cdjnvzO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
   2033 "           [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
   2034 "           [pattern ...]\n");
   2035 	fprintf(stderr,
   2036 "       pax -r [-cdijknuvzADOYZ] [-E limit] [-f archive] [-N dbdir]\n"
   2037 "           [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
   2038 "           [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
   2039 	fprintf(stderr,
   2040 "       pax -w [-dijtuvzAHLMOPX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
   2041 "           [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
   2042 "           [-U user] ... [-G group] ...\n"
   2043 "           [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
   2044 	fprintf(stderr,
   2045 "       pax -r -w [-dijklntuvzADHLMOPXYZ] [-N dbdir] [-p string] ...\n"
   2046 "           [-s replstr] ... [-U user] ... [-G group] ...\n"
   2047 "           [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
   2048 	exit(1);
   2049 	/* NOTREACHED */
   2050 }
   2051 
   2052 /*
   2053  * tar_usage()
   2054  *	print the usage summary to the user
   2055  */
   2056 
   2057 void
   2058 tar_usage(void)
   2059 {
   2060 	(void)fputs("usage: tar [-]{crtux}[-befhjlmopqvwzHOPSXZ014578] [archive] "
   2061 		    "[blocksize]\n"
   2062 		    "           [-C directory] [-T file] [-s replstr] "
   2063 		    "[file ...]\n", stderr);
   2064 	exit(1);
   2065 	/* NOTREACHED */
   2066 }
   2067 
   2068 #ifndef NO_CPIO
   2069 /*
   2070  * cpio_usage()
   2071  *	print the usage summary to the user
   2072  */
   2073 
   2074 void
   2075 cpio_usage(void)
   2076 {
   2077 
   2078 	(void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
   2079 		    "[-H format] [-O archive]\n"
   2080 		    "               < name-list [> archive]\n"
   2081 		    "       cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
   2082 		    "[-F archive] [-H format] \n"
   2083 		    "               [-I archive] "
   2084 		    "[pattern ...] [< archive]\n"
   2085 		    "       cpio -p [-adlLmuv] destination-directory "
   2086 		    "< name-list\n", stderr);
   2087 	exit(1);
   2088 	/* NOTREACHED */
   2089 }
   2090 #endif
   2091