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